I've got a price field/column (eg. 15.50 USD) and I wonder should the Rails datatype be string, decimal, or float?
Neither is appropriate. Prices should be stored as DECIMAL or INT, because they have a fixed number of decimal points. Using floats is not a good idea as values will get rounded. Strings are problematic because you won't be able to do comparisons.
The best type for price column should be DECIMAL. The type DECIMAL stores the value precisely. For Example - DECIMAL(10,2) can be used to store price value. It means the total digit will be 10 and two digits will be after decimal point.
add_column :table, :price, :decimal, :precision => 8, :scale => 2
The code above would be your best bet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With