2
ruby-1.8.7 > 1.55.round(1)
 => 1.6 
ruby-1.8.7 > 1.555.round(2)
 => 1.56 
ruby-1.8.7 > 1.155.round(2)
 => 1.16 
ruby-1.8.7 > 10.156.round(2)
 => 10.16
ruby-1.8.7 > 10.155.round(2)
 => 10.15 
ruby-1.8.7 > 10.165.round(2)
 => 10.16

What gives? Am I missing something?

EDIT

ruby-1.9.2 > 10.155.round(2)
 => 10.15 
ruby-1.9.2 > 10.165.round(2)
 => 10.16
tbk
  • 1,516
  • 3
  • 14
  • 21

2 Answers2

4

Floating point values are not precise. Your 10.165 is represented on paper/the screen as 10.165, but in memory it's represented as something extremely close to 10.165... whether it rounds up or down is a matter of which direction the error happens to fall in.

If you need accurate handling of fractional numbers, you can either represent them as BigDecimal (with a decimal scale/precision notation), or as Rational (with a fractional numerator/denominator notation).

d11wtq
  • 34,788
  • 19
  • 120
  • 195
2

The Python docs have a good explanation of the fundamental limitations of binary floating-point numbers: http://docs.python.org/tutorial/floatingpoint.html

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662