On Dec 22, 10:34 pm, TomRossi7 <[email protected]> wrote:
> Any idea why this calculates the integer the way it does?
>
> irb> ("291.15".to_f * 100.0).to_i
> => 29114

As Marnen's already pointed out, the issue is that the result is a
*tiny* bit smaller than 29115, so to_i truncates off the entire
fractional part and gives the wrong answer. In general, if you want to
convert a decimal to an integer the way most people would on paper,
use .round.to_i, which will correctly round the float to the nearest
integer.

On the currency issue, it's typically a good idea to avoid floating-
point throughout the process if you're trying to get a reliable fixed-
point result. Decimal columns are definitely a good idea for this sort
of thing.

--Matt Jones

-- 
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.

Reply via email to