Which type (Float or decimal) is best used to store prices in a mysql database?
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.
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.
Float uses 32 bits to represent data. Double uses 64 bits to represent data. Decimal uses 128 bits to represent data.
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.
Floats are not exact and can introduce cumulative rounding errors. Decimal is the best format for financial information that must be exact.
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