Possible Duplicate:
ruby floating point errors
In Ruby, when subtracting 4.7 from 5.0, I would expect a result of 0.3 but get:
~ $ irb
1.9.2p290 :001 > 5.0 - 4.7
=> 0.2999999999999998
I’m guessing there is a reason for this rather than it being a bug? Using BigDecimal objects yields the same result. Is my only option to use round on the result?
Floats lose precision. Nothing can be done about that. So, use:
and if you had needed more precision: