<p>What you want is a MySQL Decimal field.</p>

<p>Specifically you want a DECIMAL(16,8)</p>

<p>That will give you 8 digits before the decimal and 8 after the decimal</p>

<p>See <a href="http://dev.mysql.com/doc/refman/5.1/en/precision-math-decimal-changes.html">http://dev.mysql.com/doc/refman/5.1/en/precision-math-decimal-changes.html</a> for details.</p>

CC BY-SA 3.0
2018-01-04T09:35:00.227Z
What makes you believe the value won't rise above 10^8 USD? :)
Gili
CC BY-SA 3.0
2018-03-10T05:54:39.213Z
since there can't be more than 21 millions BTC, (7,8) should be enough. but if there is a possibility of treating additive values, say `volume` exchanged over a year, then it can go way over.
v.oddou
CC BY-SA 4.0
2019-03-01T13:37:00.323Z
storing ohlc base and quote volume in this format explodes the memory big time especially for 1m candles, perhaps keep ohlc float and volumes in double?
PirateApp
CC BY-SA 4.0
2021-01-08T19:59:04.207Z
@Gili The question is about storing an amount of BTC, not how much BTC is worth in fiat. Since the max amount of bitcoin possible is 21,000,000 8 digits before the decimal is sufficient.
antriver
CC BY-SA 3.0
2014-03-04T03:02:30.893Z
2014-03-04T03:02:30.893Z
Steven Potter
<p>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.</p>

CC BY-SA 3.0
2018-03-10T05:57:48.163Z
unusable. floats become very sparse at higher values. the 8 digits after the dot can't be retained after a few hundreds. it becomes integer between 8 and 16 million, and after that you have to jump 2 by 2 until the max 21 million of BTC. however it's possible to possess 19 million BTC and 5 satoshi. not that you would care at this point, but still people's asset can't be waived under the leg.
v.oddou
CC BY-SA 3.0
2014-04-09T05:32:16.577Z
2014-04-09T05:32:16.577Z
Isuru Fonseka
<p>There would be small expression errors. If you want to save as integers just multiply by 100000000 and save satoshis.</p>

<p>As @ypercube states MySQL BIGINT is a wise type choice for storing satoshis, MySQL INT does not have a sufficient maximum value.</p>

CC BY-SA 3.0
2014-03-03T05:01:57.897Z
I'm not entirely sure I understand your math.
Wannabee Bitcoin Dev
CC BY-SA 3.0
2014-03-03T05:35:54.330Z
@WannabeeBitcoinDev: Store the number of satoshis rather than the number of BTC, since satoshis can't be subdivided.  For example, for an amount of BTC 12.531, store the integer 1253100000.  For BTC 0.00031, store 31000.
Nate Eldredge
CC BY-SA 3.0
2014-04-09T10:26:42.883Z
This is not a good answer. `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.
ypercubeᵀᴹ
CC BY-SA 3.0
2014-04-09T12:40:45.177Z
@ypercube The answer says "integers" (versus floating point as in the question). INT, BIGINT and DECIMAL are all examples of integer types. If someone creating such a table from this answer can't figure out the appropriate integer type on their own, they shouldn't be creating database tables.
dchapes
CC BY-SA 3.0
2014-04-09T13:13:13.700Z
@dchapes: If someone knew, they wouldn't have asked a question in the first place. INT and BIGINT are integer datatypes, yes. `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.
ypercubeᵀᴹ
CC BY-SA 3.0
2014-03-03T04:14:59.470Z
2014-04-09T13:38:42.730Z
2014-04-09T13:38:42.730Z
zaph
<p>What should I be using to store the values of a wallet in a MySQL database?</p>

<p>Apparently floats are too imprecise to be storing Bitcoins, especially down to the satoshi.</p>

<p>So what should I be using? Decimal ?</p>

<satoshi>
How to store Bitcoin values in a MySQL database? floats are bad.... right?
Wannabee Bitcoin Dev
# How to store Bitcoin values in a MySQL database? floats are bad.... right?

(Asked by: username_0 on 2014-03-03)

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 ?

**Tags:** satoshi


---

## 3 answers

### Answer by username_7 on 2014-03-03. Score: 9 | Accepted answer

There 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.

> **Comments:**
> **username_0**: I'm not entirely sure I understand your math.  
> **username_8**: @username_0: Store the number of satoshis rather than the number of BTC, since satoshis can't be subdivided.  For example, for an amount of BTC 12.531, store the integer 1253100000.  For BTC 0.00031, store 31000.  
> **username_9**: This is not a good answer. `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.  
> **username_10**: @ypercube The answer says "integers" (versus floating point as in the question). INT, BIGINT and DECIMAL are all examples of integer types. If someone creating such a table from this answer can't figure out the appropriate integer type on their own, they shouldn't be creating database tables.  
> **username_9**: @username_10: If someone knew, they wouldn't have asked a question in the first place. INT and BIGINT are integer datatypes, yes. `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.  

---

### Answer by username_1 on 2014-03-04. Score: 19

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.

> **Comments:**
> **username_2**: What makes you believe the value won't rise above 10^8 USD? :)  
> **username_3**: since there can't be more than 21 millions BTC, (7,8) should be enough. but if there is a possibility of treating additive values, say `volume` exchanged over a year, then it can go way over.  
> **username_4**: storing ohlc base and quote volume in this format explodes the memory big time especially for 1m candles, perhaps keep ohlc float and volumes in double?  
> **username_5**: @username_2 The question is about storing an amount of BTC, not how much BTC is worth in fiat. Since the max amount of bitcoin possible is 21,000,000 8 digits before the decimal is sufficient.  

---

### Answer by username_6 on 2014-04-09. Score: -1

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.

> **Comments:**
> **username_3**: unusable. floats become very sparse at higher values. the 8 digits after the dot can't be retained after a few hundreds. it becomes integer between 8 and 16 million, and after that you have to jump 2 by 2 until the max 21 million of BTC. however it's possible to possess 19 million BTC and 5 satoshi. not that you would care at this point, but still people's asset can't be waived under the leg.  

---

