Floating Pain

written by trotter on July 7th, 2008 @ 01:19 PM

Topper mentioned a tweet he saw to me in which someone asked why (4.6 * 100).to_i #=> 459. Though this seems like a ruby bug, it’s really just one of the annoying things you hit with rounding errors and floats. At issue is that #to_i floors the float, instead of rounding it. Since the value may be approximated at 459.999999, the #to_i floors it to 459. To have things work like you’d expect, use #round when converting Float to Fixnum. See below for some code examples:

  4.6.to_i           # => 4
  4.6.round          # => 5
  (4.6 * 100).to_i   # => 459
  (4.6 * 100).round  # => 460

Comments

  • baldur on 07 Jul 16:09

    Since you enjoy floating pain here is an interesting "post":http://www.danwebb.net/2008/1/2/how-good-is-this on a similar fun.

Comments are closed

Options:

Size

Colors