The best is probably decimal(19,4), or save everything in cents using an int
best data type is for storing money values
What the best data type is for storing money values in MySQL?
19,4? Come on, that's waaay too much. There is not even that much money in the world :D Also, why ,4? Unless it's for currency exchange or similar, it should be ,2.
I usually use something like 9,2
I use int, and store amount * 100, and don't bother with decimal.
But it depends the purpose. If it's for currency exchange, stock market price, or something else for which you need 4 to 6 decimals, it will be quite different.
@HFALUCAS @RoboRobok - i'm using iranian Toman and the number is like this : 15.000 or 125,000,000 if i'm using decimal , How can I change the amount this way?
If the separator change every 3 digits (like in Europe or US, but not in India), you can use number_format()
http://php.net/manual/en/function.number-format.php
Or even money_format could be more relevant : http://php.net/manual/en/function.money-format.php
Into is fine, but not for fractions of a cent. Ie gas station.
@JLRDW - what is your opinion? Which data type do I use? I got a lot of comments , like int, decimal and ...
I use decimal. But if you will never deal with fractions of a cent int is okay.
Just don't forget to do conversions like 100 would be $1, 127 is $1.27, etc.
Well the best way is always to use integer from what you can read everywhere.
I stored it as an integer, and multiplied by currencies lowest unit (cents for dollars) but had a problem when storing cryptocurrencies, for example, ethereum smallest unit is wei, and 1 eth = 10^18 wei. So even if you're using big unsigned integer you're gonna have a problem.
First, look to your countries (or if multi-currency all the countries) GAAP rules for the number of places to go out to for a currency. Assuming this isn't a banking or stock market app as those need more precision, then the GAAP precision is what you aim for.
Then you need to decide on Decimal or Int. If it is int then you need to convert the currency to int according to the number of place per above.
For example, USA GAAP says 6 decimal places is sufficient precision for rounding. Say I have customers that are billed $0.0016 per minute for local and long distance. I would convert that as 0.0016 * 10^6 and store as integer or use a decimal field. If decimal field I just go # of places + 13, eg., DECIMAL(19,6) as that is sufficient for most.
Please or to participate in this conversation.