user211662 user211662 - 1 year ago 141
Ruby Question

Rounding float in Ruby

I'm having problems rounding. I have a float, which I want to round to the hundredth of a decimal. However, I can only use

which basically turns it into an int, meaning
2.34.round # => 2.
Is there a simple effect way to do something like
2.3465 # => 2.35

Answer Source

When displaying, you can use (for example)

>> '%.2f' % 2.3465
=> "2.35"

If you want to store it rounded, you can use

>> (2.3465*100).round / 100.0
=> 2.35
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download