<p>You can use the same type of bitcoin-core for store the value of the satoshi, so int64_t. The number is always int, I think you can use the type long because also the RPC framework response use the conversion in Bitcoin and the wrapper libraries used the type long</p>

CC BY-SA 4.0
2019-08-09T15:05:40.950Z
Can you explain your remark about `long` further?  On some systems, `long` is a 32-bit type, so I don't see how it could be safe to use it for a number of satoshis.
Nate Eldredge
CC BY-SA 4.0
2019-08-09T11:53:45.710Z
2019-08-09T11:53:45.710Z
vincenzopalazzo
<blockquote>
<p><strong>I want to do program</strong> that doing some calculations with bitcoin values.</p>
<p>I know bitcoin have 8 digits after decimal point (please correct me).</p>
</blockquote>
<p>There is no decimal in bitcoin at code level. There is no &quot;bitcoin&quot; unit either. There is only satoshi. And it is best to always stick to integer types instead of floating point types for calculations.</p>
<blockquote>
<p>Shall I use int64_t instead? It generally can store 18 digit number, so if I multiply by 100,000,000, I will be able to work in satoshi units and never lose precision.</p>
</blockquote>
<p>The number of digits doesn't matter, the size of the number in binary matters.<br />
Since the maximum value you can have as &quot;amount&quot; for bitcoin is 2100000000000000 (21 million bitcoin or 21 million * 10^8 satoshi) and this value is 7 bytes (0x0775f05a074000), you need an 8 byte (64 bit) integer. So your options are a signed or unsigned 64-bit integer value type. The sign doesn't matter because the most significant bit is never set due to the size of the max value.</p>
<p>By the way a 64-bit signed integer has a max value of 9,223,372,036,854,775,807 which is 19 digits.</p>

CC BY-SA 4.0
2019-08-11T05:37:11.767Z
Note that while this is generally sane advice, there are scenarios where you will exceed 21,000,000 BTC - for example, summing up the total transaction volume in a given time period. Choosing your data type should consider whether these scenarios are possible prior to writing any code.
Raghav Sood
CC BY-SA 4.0
2019-08-11T04:51:05.483Z
2019-08-11T04:51:05.483Z
2020-06-16T11:10:03.350Z
Coding Enthusiast
<p>I want to do program that doing some calculations with bitcoin values.</p>

<p>I know bitcoin have 8 digits after decimal point (please correct me).</p>

<p>If I store the value as <code>double</code> I worry that I might lose precision when I do the calculations.</p>

<p>Shall I use <code>int64_t</code> instead? It generally can store 18 digit number, so if I multiply by 100,000,000, I will be able to work in satoshi units and never lose precision.</p>

<p>After that I will need to divide by 100,000,000 so I can print it.</p>

<p>And what about gcc <code>quadruple precision</code>?</p>

<p>Or shall I use GNU MP library instead?</p>

CC BY-SA 4.0
2019-08-09T09:17:21.373Z
You can use the same type of bitcoin-core for store the value of the satoshi, so int64_t. The number is always int, I think you can use the long type because also the bitcoin core   RPC framework response use the conversion in Bitcoin and the wrapper libraries used the long type
vincenzopalazzo
<c++><c>
Bitcoin value as int64_t or as double
Nick
# Bitcoin value as int64_t or as double

(Asked by: username_0 on 2019-08-09)

I want to do program that doing some calculations with bitcoin values.

I know bitcoin have 8 digits after decimal point (please correct me).

If I store the value as `double` I worry that I might lose precision when I do the calculations.

Shall I use `int64_t` instead? It generally can store 18 digit number, so if I multiply by 100,000,000, I will be able to work in satoshi units and never lose precision.

After that I will need to divide by 100,000,000 so I can print it.

And what about gcc `quadruple precision`?

Or shall I use GNU MP library instead?

**Tags:** c++, c

> **Comments:**
> **username_1**: You can use the same type of bitcoin-core for store the value of the satoshi, so int64_t. The number is always int, I think you can use the long type because also the bitcoin core   RPC framework response use the conversion in Bitcoin and the wrapper libraries used the long type  

---

## 2 answers

### Answer by username_1 on 2019-08-09. Score: 0 | Accepted answer

You can use the same type of bitcoin-core for store the value of the satoshi, so int64\_t. The number is always int, I think you can use the type long because also the RPC framework response use the conversion in Bitcoin and the wrapper libraries used the type long

> **Comments:**
> **username_2**: Can you explain your remark about `long` further?  On some systems, `long` is a 32-bit type, so I don't see how it could be safe to use it for a number of satoshis.  

---

### Answer by username_3 on 2019-08-11. Score: 4

> **I want to do program** that doing some calculations with bitcoin values.
>
> I know bitcoin have 8 digits after decimal point (please correct me).

There is no decimal in bitcoin at code level. There is no "bitcoin" unit either. There is only satoshi. And it is best to always stick to integer types instead of floating point types for calculations.

> Shall I use int64\_t instead? It generally can store 18 digit number, so if I multiply by 100,000,000, I will be able to work in satoshi units and never lose precision.

The number of digits doesn't matter, the size of the number in binary matters.  
Since the maximum value you can have as "amount" for bitcoin is 2100000000000000 (21 million bitcoin or 21 million \* 10^8 satoshi) and this value is 7 bytes (0x0775f05a074000), you need an 8 byte (64 bit) integer. So your options are a signed or unsigned 64-bit integer value type. The sign doesn't matter because the most significant bit is never set due to the size of the max value.

By the way a 64-bit signed integer has a max value of 9,223,372,036,854,775,807 which is 19 digits.

> **Comments:**
> **username_4**: Note that while this is generally sane advice, there are scenarios where you will exceed 21,000,000 BTC - for example, summing up the total transaction volume in a given time period. Choosing your data type should consider whether these scenarios are possible prior to writing any code.  

---

