T
T
Theory Theory2022-02-02 09:42:46
Database design
Theory Theory, 2022-02-02 09:42:46

How to store money in int in database?

I wondered how to store money in the database. Most often, it is advised to store in int (multiply, divide by 100)

But if you replenish the account by 0.333 dollars, when multiplied by 100 we get 33.3

As I understand it, in order to make a universal option for all currencies, you need to multiply by 100000000 to take into account even bitcoin?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
@
@insighter, 2022-02-02
@Narbek

Most often it is advised to store in int

What bad advice. Any DBMS allows you to specify the precision of the number after the decimal point, use this.
If I'm not mistaken, for all the money in the world, two decimal places are enough. Bitcoin is not money. )))
Well, of course, if the program has a specific type of exchange rates, then the accuracy is considered up to 4 and up to 6 characters. What for all to dump in a heap and to do the general-purpose decision. Decide what exactly your program will store.
PS it is clear how to store monetary values. But it is better to work with them as decimal fractions, and not as floating point numbers. At least on the backend, at least on the front.

A
Alexander Shipin, 2022-02-02
@alexshipin

Answer here

M
Maxim Tkachev, 2022-02-02
@Real_Farmer

Yes. everything is correct to translate into pennies

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question