Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Float or decimal for prices?

Tags:

Which type (Float or decimal) is best used to store prices in a mysql database?

like image 967
tom Avatar asked Sep 22 '10 11:09

tom


People also ask

Is decimal better than float?

Use decimals when precision matters, such as with financial calculations. Decimals can suffer from their own precision issues, but generally, decimals are more precise than floats.

What data type is used for price?

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.

What is the difference between decimal and float?

Float uses 32 bits to represent data. Double uses 64 bits to represent data. Decimal uses 128 bits to represent data.

Why you should never use float and double for monetary calculations?

All floating point values that can represent a currency amount (in dollars and cents) cannot be stored exactly as it is in the memory. So, if we want to store 0.1 dollars (10 cents), float/double can not store it as it is.


1 Answers

Floats are not exact and can introduce cumulative rounding errors. Decimal is the best format for financial information that must be exact.

like image 76
MaxVT Avatar answered Oct 11 '22 22:10

MaxVT