views:

176

answers:

3

hello,

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 .round 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

thank you

+2  A: 

When displaying, you can use (say)

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

If you want to store it rounded, you can use

>> (2.3465*100).round / 100.0
=> 2.35
Peter
great answer thank you, I knew there was a simple way to do it.
A: 

what about (2.3465*100).round()/100?

thenoviceoof
this won't work - it will use integer division and give you `2`.
Peter
you could use 100.0
thenoviceoof
+1  A: 

Pass an argument to round containing the number of decimal places to round to

>> 2.3465.round
=> 2
>> 2.3465.round(2)
=> 2.35
>> 2.3465.round(3)
=> 2.347
Steve Weet
This would seem more sensible than multiplying, rounding and dividing. +1
Splash