number_to_currency nice, but it can get expensive; you may want to collapse yourself if you need to call it many times.
You should be aware that using float to store currency can be problematic ( and see ) if you do a lot of calculations based on these values. One solution is to use integers for currency and cents. This is similar to the approach used by the plugin. Another solution is to use the decimal type in your migration, which should work ready for modern versions of Rails (> 1.2):
add_column :items, :price, :decimal, :precision => 10, :scale => 2
( :scale - the number of places that have passed after the decimal place :precision - the total number of digits.) This will allow you to BigDecimal objects in Rails, which are a little more difficult to work with, but not so bad.
Both integer and decimal approaches are slightly slower than floating point. I use currency floats in some places because I know that I donβt need to do value calculations in Rails, but only store and display them. But if you need accurate currency calculations, do not use floats.
source share