To store monetary values one should avoid the type of data float
(floating point). This goes for any language. As for the type of data money
, works correctly, with 4 decimal places fixed.
By example you need 6 decimal places, which eliminates the use of data type money
. In SQL Server you can use decimal (p, s), where p is the accuracy and s is the scale. That is, use 6 as the scale. To define the precision, be aware that it is necessary to count both the whole part and the fractional part. For example, to store values up to 9,999,999,999 are 4 digits in the whole part and 6 digits in the fractional part. Therefore, the accuracy should be
4 + 6 = 10
That is to say, decimal (10, 6)
.
As for Mysql, I haven’t used it for some time, except for sporadic testing. But, referring to the documentation, it is the same rule of SQL Server regarding the definition of precision and scale.
Documentation:
Read also:
Basically what you want is to know which is the best type in the database to represent monetary values with many decimals after the comma, is that it? If it is, the fact that it is bitcoin or anything else, becomes irrelevant.
– Victor Stafusa
Exactly, I used the example of bitcoin for the number of decimals, which normally with normal monetary transactions (I think) does not have as many houses as.
– Thiago Loureiro
@Josédiz thanks, maybe I expressed myself badly talking about money type, but it is not a good practice, I always see people talking to NOT use in any way.. would you know in mysql if decimal applies the same way? obg.
– Thiago Loureiro
Possible duplicate of Best kind of data to work with money?
– vinibrsl
@vnbrs I researched about the type to work with money, but my doubt on this topic I was not sure if it would meet the amount of decimal places and precision required for my need, generating doubt.
– Thiago Loureiro
Possible duplicate of Best kind of data to work with money?
– Bacco