Reputation: 29
Databases like SQL server allow to store up to 38 digits.
This amount is insufficient for some use cases like cryptocurrency digit storage. Being ETH an example. ETH has its value declared as uint256. This represents up to 78 digits.
The only solution I have seen to avoid rounding up and losing accuracy is storing the quantities of the cryptocurrency/tokens as a string.
This is quite unpractical since you cannot then to operations in SQL like SUM().
There have to be other workarounds out there.
Similar questions asked Storing ETH precision Crypto Currency decmials
Upvotes: 0
Views: 1031
Reputation: 83438
Two solutions
PostgreSQL happily stores up to 78 digits. Please switch to a better database. PostgreSQL is not very far from MSSQL. Thus, your comment about "databases" is not correct, it is only some databases.
Store your data as a 32 bytes binary blobs BINARY(32)
and convert numbers to the decimals at the application level. This is much more efficient than a string. However, you still would need to do SUM()
on the application level, or script at the database level. Switching to a better database is much easier.
Upvotes: 1
Reputation: 29
One solution I figured out is to divide the number in different columns and then making the necessary operations on each column. Later summing up all the columns.
This solution is not perfect as the database will not store up anyway the 78 digits, but brings me closer to the real number.
Storing the numbers in a string and then casting the strings to number format is not ideal and I would lose decimal accuracy per row.
None of the solutions will give me the real accurate number, but by dividing the number in columns I lose less accuracy.
Upvotes: 0