<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://en.zaoniao.it/index.php?action=history&amp;feed=atom&amp;title=Proper_Money_Handling_%28JSON-RPC%29</id>
	<title>Proper Money Handling (JSON-RPC) - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://en.zaoniao.it/index.php?action=history&amp;feed=atom&amp;title=Proper_Money_Handling_%28JSON-RPC%29"/>
	<link rel="alternate" type="text/html" href="http://en.zaoniao.it/index.php?title=Proper_Money_Handling_(JSON-RPC)&amp;action=history"/>
	<updated>2026-05-16T08:48:55Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.32.0</generator>
	<entry>
		<id>http://en.zaoniao.it/index.php?title=Proper_Money_Handling_(JSON-RPC)&amp;diff=6442&amp;oldid=prev</id>
		<title>Admin: Created page with &quot;== Overview == 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...&quot;</title>
		<link rel="alternate" type="text/html" href="http://en.zaoniao.it/index.php?title=Proper_Money_Handling_(JSON-RPC)&amp;diff=6442&amp;oldid=prev"/>
		<updated>2019-06-29T09:24:02Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;== Overview == 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...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&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.10 BTC.&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;amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;amp;gt;&lt;br /&gt;
 function JSONtoAmount() {&lt;br /&gt;
 printf '%.8f' &amp;quot;$1&amp;quot; | tr -d '.'&lt;br /&gt;
 }&lt;br /&gt;
&amp;amp;lt;/source&amp;amp;gt;&lt;br /&gt;
&lt;br /&gt;
== C/C++ ==&lt;br /&gt;
C/C++ JSON libraries return the JavaScript Number type as type 'double'. 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;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 Math.round(1e8 * value);&lt;br /&gt;
 }&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
 public long JSONtoAmount(double value){&lt;br /&gt;
 return (long)(value*100000000L);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Perl ==&lt;br /&gt;
 sub JSONtoAmount {&lt;br /&gt;
 return sprintf '%.0f', 1e8 * shift;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Go ==&lt;br /&gt;
The [https://github.com/btcsuite/btcd/btcjson btcjson package] provides a more complete version of this function (error checking and so on), but for illustrative purposes, this is useful.&lt;br /&gt;
&lt;br /&gt;
 func JSONToAmount(jsonAmount float64) (int64) {&lt;br /&gt;
 var amount int64&lt;br /&gt;
 tempVal := 1e8 * jsonAmount&lt;br /&gt;
 if tempVal &amp;amp;lt; 0 {&lt;br /&gt;
 tempVal = tempVal - 0.5&lt;br /&gt;
 }&lt;br /&gt;
 if tempVal &amp;amp;gt; 0 {&lt;br /&gt;
 tempVal = tempVal + 0.5&lt;br /&gt;
 }&lt;br /&gt;
 // Then just rely on the integer truncating&lt;br /&gt;
 amount = int64(tempVal)&lt;br /&gt;
 return amount&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;
== Common Lisp ==&lt;br /&gt;
 (defun json-to-amount (n)&lt;br /&gt;
 (coerce (round (* n 1e8)) 'integer))&lt;br /&gt;
&lt;br /&gt;
'''CAUTION''': The CL-JSON library parses numbers as ''single'' precision floating-point by&lt;br /&gt;
default. The default parsing behavior can be overridden as follows:&lt;br /&gt;
&lt;br /&gt;
 (set-custom-vars :real (lambda (n)&lt;br /&gt;
 (json::parse-number (concatenate 'string n &amp;quot;d0&amp;quot;))))&lt;br /&gt;
&lt;br /&gt;
==Source==&lt;br /&gt;
&lt;br /&gt;
[http://bitcoin.it/ http://bitcoin.it/]&lt;br /&gt;
&lt;br /&gt;
==See Also on BitcoinWiki==&lt;br /&gt;
* [[CoinUs]]&lt;br /&gt;
* [[Treon]]&lt;br /&gt;
* [[DeHedge]]&lt;br /&gt;
* [[Friend]]&lt;br /&gt;
* [[MulTra]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Technology]]&lt;/div&gt;</summary>
		<author><name>Admin</name></author>
		
	</entry>
</feed>