What should I be using to store the values of a wallet in a MySQL database?
Apparently floats are too imprecise to be storing Bitcoins, especially down to the satoshi.
So what should I be using? Decimal ?
Bitcoin Stack Exchange is a question and answer site for Bitcoin users, developers, and enthusiasts. It only takes a minute to sign up.
Sign up to join this communityThere would be small expression errors. If you want to save as integers just multiply by 100000000 and save satoshis.
As @ypercube states MySQL BIGINT is a wise type choice for storing satoshis, MySQL INT does not have a sufficient maximum value.
INT
datatype in MySQL is restricted up to 2^32-1 for UNSIGNED
and from -2^31 to 2^31-1 in SIGNED
. BIGINT
or DECIMAL
is a wiser choice.
Apr 9, 2014 at 10:26
DECIMAL
is a fixed point but not integer. And someone who doesn't know the details, may confuse INTEGER
with integer. I only meant this comment to notify the answerer, so he can improve the asnwer. I have not downvoted as I agree with the general notion of the answer.
Apr 9, 2014 at 13:13
What you want is a MySQL Decimal field.
Specifically you want a DECIMAL(16,8)
That will give you 8 digits before the decimal and 8 after the decimal
See http://dev.mysql.com/doc/refman/5.1/en/precision-math-decimal-changes.html for details.
volume
exchanged over a year, then it can go way over.
a float natively only goes up to 7 decimals in most cases. if you just roll with decimal you'll be safe. When you round a 7 digit number to 8 decimal places you'll run into rounding issues.