Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String, decimal, or float datatype for price field?

I've got a price field/column (eg. 15.50 USD) and I wonder should the Rails datatype be string, decimal, or float?

like image 275
never_had_a_name Avatar asked Jun 17 '10 17:06

never_had_a_name


People also ask

Should Price be string or number?

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.

What is the data type for price in SQL?

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.


1 Answers

add_column :table, :price, :decimal, :precision => 8, :scale => 2 

The code above would be your best bet.

like image 140
Tom Avatar answered Oct 11 '22 03:10

Tom