If you're working with currency, it's usually better to store it and
do calculations as integers and then convert to decimal for display.

Jarin

On Dec 14, 2:43 pm, "William Cassis" <[email protected]>
wrote:
> Hi folks,
>
> Can anyone help explain the following Ruby (specifically ruby 1.9.1p243
> (2009-07-16 revision 24175)) Float rounding behavior?:
>
> > irb(main):001:0> 0.555.round(2)
> > => 0.56
>
> Looks correct
>
> > irb(main):002:0> 0.565.round(2)
> > => 0.56
>
> Why?
>
> > irb(main):003:0> 0.575.round(2)
> > => 0.57
>
> Why?
>
> > irb(main):004:0> 0.585.round(2)
> > => 0.59
>
> Looks correct
>
> If I instead convert the decimals above into BigDecimal objects and then
> perform the rounding operation it produces the "expected" output
> everytime.
>
> The rounding example below happens on both Ruby 1.9 and 1.8.6 (ruby
> 1.8.6 (2008-08-11 patchlevel 287)):
>
> > irb(main):001:0> 57.5.class
> > => Float
> > irb(main):002:0> 57.5.round
> > => 58
>
> Looks correct
>
> > irb(main):003:0> (Float(0.575) * (10 ** 2))
> > => 57.5
> > irb(main):004:0> (Float(0.575) * (10 ** 2)).class
> > => Float
> > irb(main):005:0> (Float(0.575) * (10 ** 2)).round
> > => 57
>
> Why?
>
> Thanks for your help!

-- 
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby

Reply via email to