My application deals a lot with money. I'm currently using BigDecimal
(SQL decimal) to represent money, which is kind of a hassle because of
all the BigDecimal.new('...') calls. For this reason, I'm considering
switching over to float. Is there any reason not to do so?
There are millions of rounding-off reasons not to do so. Or use integers and fixed point.
This is actually one of the areas where I severely miss the automatic variable-precision handling of numbers in Smalltalk.
If you’ve used a decimal datatype [with the necessary precision and scope options] in your db, Rails should be able to handle managing the decimal part for you. You might still need to use number_to_currency and formatting in your views but AFAIK Rails is able to handle the conversions on its own and save you the work.
zomg rsl posts on the rubyonrails-talk mailing list!
For money I use a single integer field, sometimes cost_in_cents, and then use the Money gem to parse that to a real format. Thankfully I haven’t yet to deal with fractions of a cent. If you’re not worried about fractions of a cent, then go for the Money gem and an integer field.
i haven’t worked with currency a lot myself. what are the benefits of using the money gem over decimal datatype? or just another tool? i’ve seen it touted before and wondered. no place like the mailing list to get some perspectives.
Well, my use of money gem and integer for currency amounts stems from
the fact that the ActiveMerchant plugin works with those. Over time,
though, I've found it to be a very dev-friendly solution.
I'm not sure if I can come up with an overriding, grand reason for using
one or the other, tbh.
RSL ___ wrote:
disclaimer needed - I haven't worked with global currency issues and
thus need to wrap my comments with <USD> </USD>.