Prices in Cents
In the second edition of AWDwR, I changed
the Depot application to store money in integral cents, rather than
floating point dollars and cents. Some folks pushed back on this, saying it
overly complicated the application.
This morning, while working on some code to migrate some legacy data, I bumped into a problem that boiled down to the following (on my PPC—your results may differ)
dave[store/migrate 9:00:57] irb irb(main):001:0> Integer(77.85 * 100.0) => 7784
Obviously it’s possible to do rounding (although even that’s trickier than you might first code if you have to handle negative numbers).
I still feel that Rails should convert decimal(x,y) columns into BigDecimal format in Ruby, giving us scaled, exact, representations. Until that day, stick with integers for amounts that you need to be exact.




Comments