TomRossi7 wrote in post #970207:
> Any idea why this calculates the integer the way it does?
>
> irb> ("291.15".to_f * 100.0).to_i
> => 29114
Because of the vagaries of floating-point arithmetic:
irb> ("291.15".to_f * 100.0) < 29115
=> true
irb> printf "%.30f", ("291.15".to_f * 100.0)
29114.999999999996362021192908287048
It bears repeating: never, ever use Floats for arithmetic. If you can't
use fixed-point math, then use BigDecimal.
>
> Thanks,
> Tom
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.