I'm working on an application where we have some services that requires a monetary value to calculate budgets for services. Currently we are using columns DECIMAL(10,2)
.
While analysing some of the generated budgets I've noticed some of them returns values like 180.0000000024
.
Reading on other questions, I've only found the same type of answers, to go with decimals. In a few cases I saw a recomendation to use INT
(or similar) instead. The reason is because the calculations will be more precise.
Example: $64.58
+ $348.99
would be calculated like 6458
+ 34899
and stored the result 41357
and later on converted to 413.57
.
So it left mw with this questions, on which is the best way to store currency values on the database. Is DECIMAL
really the best way? How much of a problem those extra decimals values can be a problem? Is INT
(or similar) type going to improve something?
Best Answer
Do not use
FLOAT
orDOUBLE
. If it is a single currency, then useDECIMAL(m,n)
withm
andn
suitably chosen. If different currencies are stored in the same column, values form
andn
become messy. And you can run into rounding issues -- because of different laws.DECIMAL
goes through a package that doesINT
-like stuff transparently.DECIMAL(19,4)
will be stored in