<p>bitcoind internally treats all amounts as an integer and only displays the value as a decimal for the RPC. Formatting for display does not introduce any floating point precision issues as it still just does integer conversion with some string formatting to make it look like a decimal. So it is perfectly safe to multiply by <code>100000000</code> to get an integer amount of satoshis.</p>

CC BY-SA 4.0
2021-03-29T05:18:55.693Z
Thank you! However, it seems to me that RPC values come from the AmountFromValue() function, that makes a conversion to double and a division. I still think that multiplying by `100000000` works, but I am curious of your claim about string formatting. May you please elaborate?
Matthieu Latapy
CC BY-SA 4.0
2021-03-29T16:13:14.187Z
The functions to look at are `FormatMoney`, `ParseMoney`, and `AmountFromValue`. None of these functions use floats or doubles. `FormatMoney` is how the RPC converts integer amounts to decimal amounts for display. It does integer division and modulo, then string formatting to make it look like a decimal.
Ava Chow
CC BY-SA 4.0
2021-03-29T18:05:35.293Z
Right, I was looking at an old version of the code, with double conversions and divisions, but in modern implementations I find what you describe. Thanks!
Matthieu Latapy
CC BY-SA 4.0
2021-04-01T04:05:29.820Z
Let me add that, if the "value" v is stored as a float (which we should never do), taking int(v*100000000) is incorrect; one should take int(round(v*100000000)). Both give the correct value if v=0.1, but int(4.35*100000000) gives 434999999 (incorrect) whereas int(round(4.35*100000000)) gives 435000000 (correct), at least in my settings.
Matthieu Latapy
CC BY-SA 4.0
2024-05-15T13:44:34.763Z
Why do we use decimal values for the RPC? Why not just follow with the amount in satoshis?
polespinasa
CC BY-SA 4.0
2021-03-28T21:40:11.917Z
2021-03-28T21:40:11.917Z
Ava Chow
<p>It seems that RPC calls for getting JSON information on blocks return &quot;value&quot; fields in bitcoins, not in satoshis.</p>
<p>This means that <em>there is a conversion from integer to float somewhere</em>, which is disturbing since it may introduce rounding errors (see context below).</p>
<p><strong>Is there any way to get the <em>integer</em> satoshi amounts directly in the JSON?</strong></p>
<p>Otherwise, is it <em>completely</em> safe to just multiply the float value by 10^8 and take the integer part?</p>
<p>Is there a better way to directly obtain the integer satoshi value?</p>
<p>Thanks!</p>
<hr />
<p>Let me give some context.</p>
<p>A typical concern is that, for instance, most systems have no float of value (exactly) 0.1; they generally have a (tiny) difference.</p>
<p>On my machine, I obtain:</p>
<pre><code>python3.6 -c 'print(&quot;%.20f&quot;%0.1)'
0.10000000000000000555

python3.6 -c 'print(0.1+0.1+0.1==0.3)'
False
</code></pre>
<p>This is generally safe, but in my context this raises messy issues, so dealing only with integers is appealing.</p>

CC BY-SA 4.0
2021-03-28T19:48:17.490Z
Here is a related discussion, but it seems that no consensus was reached at that time: https://github.com/bitcoin/bitcoin/issues/3249
Matthieu Latapy
<json-rpc><value><raw-data>
How to get _integer_ satoshi values from JSON RPC?
Matthieu Latapy
# How to get _integer_ satoshi values from JSON RPC?

(Asked by: username_0 on 2021-03-28)

It seems that RPC calls for getting JSON information on blocks return "value" fields in bitcoins, not in satoshis.

This means that *there is a conversion from integer to float somewhere*, which is disturbing since it may introduce rounding errors (see context below).

**Is there any way to get the *integer* satoshi amounts directly in the JSON?**

Otherwise, is it *completely* safe to just multiply the float value by 10^8 and take the integer part?

Is there a better way to directly obtain the integer satoshi value?

Thanks!

---

Let me give some context.

A typical concern is that, for instance, most systems have no float of value (exactly) 0.1; they generally have a (tiny) difference.

On my machine, I obtain:

```
python3.6 -c 'print("%.20f"%0.1)'
0.10000000000000000555

python3.6 -c 'print(0.1+0.1+0.1==0.3)'
False

```

This is generally safe, but in my context this raises messy issues, so dealing only with integers is appealing.

**Tags:** json-rpc, value, raw-data

> **Comments:**
> **username_0**: Here is a related discussion, but it seems that no consensus was reached at that time: https://github.com/bitcoin/bitcoin/issues/3249  

---

## 1 answers

### Answer by username_1 on 2021-03-28. Score: 1 | Accepted answer

bitcoind internally treats all amounts as an integer and only displays the value as a decimal for the RPC. Formatting for display does not introduce any floating point precision issues as it still just does integer conversion with some string formatting to make it look like a decimal. So it is perfectly safe to multiply by `100000000` to get an integer amount of satoshis.

> **Comments:**
> **username_0**: Thank you! However, it seems to me that RPC values come from the AmountFromValue() function, that makes a conversion to double and a division. I still think that multiplying by `100000000` works, but I am curious of your claim about string formatting. May you please elaborate?  
> **username_1**: The functions to look at are `FormatMoney`, `ParseMoney`, and `AmountFromValue`. None of these functions use floats or doubles. `FormatMoney` is how the RPC converts integer amounts to decimal amounts for display. It does integer division and modulo, then string formatting to make it look like a decimal.  
> **username_0**: Right, I was looking at an old version of the code, with double conversions and divisions, but in modern implementations I find what you describe. Thanks!  
> **username_0**: Let me add that, if the "value" v is stored as a float (which we should never do), taking int(v*100000000) is incorrect; one should take int(round(v*100000000)). Both give the correct value if v=0.1, but int(4.35*100000000) gives 434999999 (incorrect) whereas int(round(4.35*100000000)) gives 435000000 (correct), at least in my settings.  
> **username_2**: Why do we use decimal values for the RPC? Why not just follow with the amount in satoshis?  

---

