Montag, August 04, 2008

Annoying floating point conversion in Ruby

Try (4.85*100).to_i in irb and you will get:

This is weired and totaly unexpected for me. I understand that floating points are not accurate and conversions have their limits. Type "%.32f" % 4.85 and you see the real value of this float. The reason is that to_i does just a truncate.

If you want the expected result you have to use:

Keine Kommentare: