You get a bonus - 1 coin for daily activity. Now you have 1 coin

Data types for storing currencies, money in code and in a database. The best?

Lecture



We need to understand and choose how to store and work with money.
After the first analysis of the problem, we find out that there are several sub-problems-

1) how to reverse fractional cash assets?

2) at what point to round?

3) how to store (as an integer, as a floaf, as a special type, as a text value of VARCHAR, etc. )?

You can store it in the database using the following methods:


1) Storage in int (bigint) (support all kinds of databases)
2) Storage in decimal (Mysql, Postgess) or equivalent ("Currency" - MS Sql)

Suppose that it is enough for us to conduct billing in the system in USD with an accuracy of cents.



Storing money in BIGINT (INT)

Pros :

  • No floating point - less inaccuracies
  • You can perform standard mathematical operations and not be afraid that you’ll lose a penny somewhere
  • Mathematical (during calculations) operations are faster
  • Reporting selections will be faster
  • Here is the international currency standard en.wikipedia.org/wiki/ISO_4217
    It shows that the number of decimal places they can have is different. For ease of development, everything should be stored in a minimum fractional monetary unit of the currency, carrying out the conversion during output and input (i.e. in int)


Cons :

  • Remember to constantly multiply / divide by 100
  • It is possible to guess the right accuracy from the very beginning. for example, a design system using two precision signs and then a floor system will be used with the main currency bitcoins where 6 decimal places are important



Money storage in Deciamal / Numeric / Currency

Recommendations for MySQL
The DECIMAL and NUMERIC types store exact numeric data values. These types are used when it is important to maintain accurate accuracy, for example, with monetary data. In MySQL, NUMERIC is implemented as DECIMAL, so the following notes about DECIMAL apply equally to NUMERIC.

Pros:

  • Stored in its natural form
  • No additional rounding required for output and input
  • It is possible to change with time - increase or decrease accuracy
  • Compatible with GAAP. GAAP makes sure that numerical rounding errors are small enough to be considered inconsequential even when working with many records.

Minuses:

  • Inaccurate work (not through bcmath) can make mistakes when multiplying or dividing
  • There is a chance of accumulating the amount of rounding as a result of getting different amounts relative to the total
  • Work through bcmath more slowly
  • Different currencies have different number of characters, a comma field - if you have a multi-currency system, there will be data redundancy. You will have to do more than 2 decimal places, but not everyone will need them
  • One Robert Martin (Robert Cecil Martin), also known as Uncle Bob : "Using floating-point numbers to represent monetary amounts is almost a crime."

Data types for storing currencies, money in code and in a database. The best?

Which way of storage and at what point should be converted by the application architect together with the accountant of the enterprise.

In any case, it is necessary to take into account all stages of rounding and at the testing stage to identify possible problems associated with rounding and especially the accumulation of rounded amounts.

created: 2019-12-04
updated: 2024-11-13
7



Rating 9 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Structures and data processing algorithms.

Terms: Structures and data processing algorithms.