Ruby on Rails Thursday, December 23, 2010

Please quote when replying.

TomRossi7 wrote in post #970346:
> I have currency information that I plan to store as an integer --
> ironically to avoid issues like this. The user input will always be
> in a decimal form. I'm thinking I will just do this ('291.15'.to_f *
> 100).round instead. That should round out any of the weird float
> issues.

I think there are better ways of doing that -- such as using a DECIMAL
field in the database. Then no conversion is necessary and the data is
stored as fixed-point.

Or, if you'd rather store the data as an integral number of pennies
(though that may have i18n issues -- there are a few currencies, such as
the Kuwaiti dinar, that are subdivided into 1000, not 100), then a quick
way would be to normalize the string so that it contains 2 decimal
places (so "123" would become "123.00"), then drop the decimal point.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org

--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment