Money with precision
I’ve been working on a project that needs to store mileage reimbursement rates to the nearest tenth of a cent. We were using the money
gem, which stores money amounts in cents, so it looked like it was going to be a pain.
But without too much suffering, I modified the money gem to take a precision (in powers of 10), which defaults to 2. It can now store amounts in any precision.
>> amount = 20.to_money + 0.505.to_money
=> #<Money @precision=3, @currency="USD", @cents=20505>
>> amount.to_s
=> "20.505"
>> amount.format
=> "$20.51"
You can also store amounts in negative precisions, like millions:
>> amount = Money.new(50, 'USD', -6)
>> amount.to_s
=> "50"
>> amount.format
=> "$50000000.00"
Check out our fork of the money gem on github. There are lots of other goodies in there.