Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mDelshad's avatar

best data type is for storing money values

What the best data type is for storing money values in MySQL?

0 likes
11 replies
hfalucas's avatar

The best is probably decimal(19,4), or save everything in cents using an int

1 like
RoboRobok's avatar

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

2 likes
Vilfago's avatar

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.

2 likes
mDelshad's avatar

@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?

jlrdw's avatar

Into is fine, but not for fractions of a cent. Ie gas station.

mDelshad's avatar

@JLRDW - what is your opinion?   Which data type do I use? I got a lot of comments , like int, decimal and ...

jlrdw's avatar

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.

2 likes
einsteinpp's avatar

Well the best way is always to use integer from what you can read everywhere.

1 like
nsvetozarevic's avatar

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.

1 like
PatrickSJ's avatar

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.

3 likes

Please or to participate in this conversation.