<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://en.bitcoin.it/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mrvdb</id>
	<title>Bitcoin Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://en.bitcoin.it/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mrvdb"/>
	<link rel="alternate" type="text/html" href="https://en.bitcoin.it/wiki/Special:Contributions/Mrvdb"/>
	<updated>2026-04-23T13:39:03Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.8</generator>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Proper_Money_Handling_(JSON-RPC)&amp;diff=8650</id>
		<title>Proper Money Handling (JSON-RPC)</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Proper_Money_Handling_(JSON-RPC)&amp;diff=8650"/>
		<updated>2011-05-21T10:59:44Z</updated>

		<summary type="html">&lt;p&gt;Mrvdb: /* Python */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Overview ==&lt;br /&gt;
The original bitcoin client stores all bitcoin values as 64-bit integers, with 1 BTC stored as 100,000,000 (one-hundred-million of the smallest possible bitcoin unit).  Values are expressed as double-precision Numbers in the JSON API, with 1 BTC expressed as 1.00000000&lt;br /&gt;
&lt;br /&gt;
If you are writing software that uses the JSON-RPC interface you need to be aware of possible floating-point conversion issues.  You, or the JSON library you are using, should convert amounts to either a fixed-point Decimal representation (with 8 digits after the decimal point) or ideally a 64-bit integer representation. In either case, rounding values is required.&lt;br /&gt;
&lt;br /&gt;
Improper value handling can lead to embarrassing errors; for example, if you truncate instead of doing proper rounding and your software will display the value &amp;quot;0.1 BTC&amp;quot; as &amp;quot;0.09999999 BTC&amp;quot; (or, worse, &amp;quot;0.09 BTC&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
The original bitcoin client does proper, full-precision rounding for all values passed to it via the RPC interface.  So, for example, if the value 0.1 is converted to the value &amp;quot;0.099999999999&amp;quot; by your JSON-RPC library, that value will be rounded to the nearest 0.00000001 bitcoin and will be treated as exactly 0.1 bitcoins.&lt;br /&gt;
&lt;br /&gt;
The rest of this page gives sample code for various JSON libraries and programming languages.&lt;br /&gt;
&lt;br /&gt;
== BASH ==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 function JSONtoAmount() {&lt;br /&gt;
     printf &#039;%.8f&#039; &amp;quot;$1&amp;quot; | tr -d &#039;.&#039;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== C/C++ ==&lt;br /&gt;
C/C++ JSON libraries return the JavaScript Number type as type &#039;double&#039;.  To convert, without loss of precision, from a double to a 64-bit integer multiply by 100,000,000 and round to the nearest integer:&lt;br /&gt;
 int64_t JSONtoAmount(double value) {&lt;br /&gt;
     return (int64_t)(value * 1e8 + (value &amp;lt; 0.0 ? -.5 : .5));&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
To convert to a JSON value divide by 100,000,000.0, and make sure your JSON implementation outputs doubles with 8 or more digits after the decimal point:&lt;br /&gt;
  double forJSON = (double)amount / 1e8;&lt;br /&gt;
&lt;br /&gt;
== ECMAScript ==&lt;br /&gt;
&lt;br /&gt;
 function JSONtoAmount(value) {&lt;br /&gt;
     return amount = Math.round(1e8 * value);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Perl ==&lt;br /&gt;
 sub JSONtoAmount {&lt;br /&gt;
     return sprintf &#039;%.0f&#039;, 1e8 * shift;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== PHP ==&lt;br /&gt;
 function JSONtoAmount($value) {&lt;br /&gt;
     return round(value * 1e8);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Python ==&lt;br /&gt;
 def JSONtoAmount(value):&lt;br /&gt;
     return long(round(value * 1e8))&lt;br /&gt;
 def AmountToJSON(amount):&lt;br /&gt;
     return float(amount / 1e8)&lt;br /&gt;
&lt;br /&gt;
[[Category:Technical]]&lt;br /&gt;
[[Category:Developer]]&lt;/div&gt;</summary>
		<author><name>Mrvdb</name></author>
	</entry>
</feed>