agentbanks217 agentbanks217 - 1 year ago 83
Ruby Question

How to truncate a floating point number without rounding up in Ruby?

I have a floating point number that I want to truncate to 3 places but I don't want to round up. How would I go about doing this in Ruby? Any help would be much appreciated?

Ex: 1.0155555555555555 => 1.015 not 1.016

Answer Source

Assuming you have a float, try this:

(x * 1000).floor / 1000.0



See it working online: ideone

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download