Bugs item #10713, was opened at 2007-05-09 11:12
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=1698&aid=10713&group_id=426

Category: None
Group: None
Status: Closed
Resolution: None
Priority: 3
Submitted By: john oordopjes (oordopjes)
Assigned to: Nobody (None)
Summary: Weird rounding behavior..

Initial Comment:
irb(main):002:0> n=4
=> 4
irb(main):003:0> a=(52.07825 * (10.0 ** n))
=> 520782.5
irb(main):004:0> a.round
=> 520782

Which is WRONG it should be 520783.
Using an other number:

irb(main):005:0> a=(52.07835 * (10.0 ** n))
=> 520783.5
irb(main):006:0> a.round
=> 520784

This is correct behavior.. really WEIRD isn't it?

----------------------------------------------------------------------

Comment By: Roel Harbers (rharbers)
Date: 2007-05-10 08:17

Message:
To expand on Nobuyoshi's answer, it's not a bug, it's called banker's rounding. When encountering a .5 value, round towards the nearest even number. This is to prevent rounding bias, and is a common way to round in most languages I know.

Additionally, rounding of floating point values can look deceiving, because what looks like 7.5 might actually be 7.499999, so it should be rounded towards 7.

See http://en.wikipedia.org/wiki/Rounding for more information.

----------------------------------------------------------------------

Comment By: Nobuyoshi Nakada  (nobu)
Date: 2007-05-10 02:10

Message:
Not a bug.

----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=1698&aid=10713&group_id=426