Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two rounding methods give different results

Two different rounding methods of floats show different results.

"%.2f" % 0.015 # => "0.01" 
0.015.round(2) # => 0.02 

One is string and the other a float. When rounding anything but 0.5, it rounds correctly or rather the same way as the round function.

"%.2f" % 0.01500000000000001 # => "0.02" 

Also, it doesn't always behave like that:

[0.005, 0.015, 0.025, 0.035, 0.045, 0.055, 0.065, 0.075, 0.085, 0.095].map { |x| "%.2f" % x}
# => ["0.01", "0.01", "0.03", "0.04", "0.04", "0.06", "0.07", "0.07", "0.09", "0.10"]

I am not sure if this is technically a bug, but at least it is very counter intuitive. Does anybody know why two rounding methods act that differently?

like image 707
Lapis Avatar asked Dec 07 '25 23:12

Lapis


1 Answers

Floating values aren't always stored exactly -- they are instead stored as a base and exponent which for some numbers is stored exactly and others is not. Any modifications to the float can cause a very small fraction to appear. Thus rounding something right at a boundary with different rounding functions can make it switch between the two possible results.

Doing "%.2f" % 0.015.round(2) will give you the 0.02 result you wanted, as I am guessing the %.2f is implemented differently than the floating round method.

like image 59
Pyrce Avatar answered Dec 09 '25 15:12

Pyrce



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!