Follow Along as I Stumble on the Path to Learning RoR

Number Types in Rails

I mistakenly had some fields as integers, but they needed decimals.  I did some reading and found that people tended to favor :decimal over :float because :float numbers sometimes had errors.  Some people favored parsing  the numbers before the decimal, and the ones after and storing each as an integer.

I decided just to go with :decimal since most people recommended that.

For dollars (price), I used:

t.decimal :price, :precision => 16, :scale => 2

For sales tax, I used:

t.decimal :tax_percent, :precision => 6, :scale => 4

Precision is the amount of digits you can have in total, while scale indicates how many places you can have after the decimal point.  As someone pointed out, make sure that the precision number is more than the scale number.


Comments are closed.