Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: Nefario on April 12, 2011, 05:17:53 AM



Title: Float value from bitcoind, how should I represent it?
Post by: Nefario on April 12, 2011, 05:17:53 AM
I've read https://en.bitcoin.it/wiki/Proper_Money_Handling_(JSON-RPC) but am still unsure as to what to do.

I'm using the JSON library in ruby to parse the json result from bitcoind

And for things like balance it will return a float.

I am wondering how best to proceed with the representation of bitcoin balances or numbers in general in my application.

Should I leave it as a float(from what I've read it's not recommended)?
Convert it to BigDecimal
Convert it to an integer (this would mean multiplying it by 100,000,000 right)?

What experience has anyone else had with this, which did they choose and why?

Whats the simplest option?


Title: Re: Float value from bitcoind, how should I represent it?
Post by: xf2_org on April 12, 2011, 06:52:47 AM
I've read https://en.bitcoin.it/wiki/Proper_Money_Handling_(JSON-RPC) but am still unsure as to what to do.

Unfortunately that wiki page has been changed to reflect a minority opinion, and nobody has the time or motivation to make corrective edits that will then be immediately reverted.

If your programming language supports it, you should use a fixed-precision data type such as decimal.



Title: Re: Float value from bitcoind, how should I represent it?
Post by: theymos on April 12, 2011, 12:19:06 PM
After converting to decimal, make sure you round to 8 decimals of precision to fix any float errors.


Title: Re: Float value from bitcoind, how should I represent it?
Post by: Nefario on April 12, 2011, 12:36:00 PM
Well since I've already built my system to handle integers that's what I'm going with.
First I'm converting the float to a string representation, then using that to make a object of BigDecimal(with 8 places for correct representation), and then multiplying by 100,000,000.

Everything else is done internally with integers.


Title: Re: Float value from bitcoind, how should I represent it?
Post by: Gavin Andresen on April 12, 2011, 01:46:05 PM
First I'm converting the float to a string representation, then using that to make a object of BigDecimal(with 8 places for correct representation), and then multiplying by 100,000,000.

Everything else is done internally with integers.

That sounds overly complicated.  Does your ruby/json implementation have double-precision (64-bit) floats?  If it does, just multiply by 1.0e8 and rounding to the nearest integer.

Quick way to tell if your ruby implementation does 64-bit floats:  see what you get converting 21000000.00000001*1e8 to an integer.


Title: Re: Float value from bitcoind, how should I represent it?
Post by: Nefario on April 12, 2011, 01:55:10 PM
First I'm converting the float to a string representation, then using that to make a object of BigDecimal(with 8 places for correct representation), and then multiplying by 100,000,000.

Everything else is done internally with integers.

That sounds overly complicated.  Does your ruby/json implementation have double-precision (64-bit) floats?  If it does, just multiply by 1.0e8 and rounding to the nearest integer.

Quick way to tell if your ruby implementation does 64-bit floats:  see what you get converting 21000000.00000001*1e8 to an integer.

Ruby has 2 classes for representing numbers, Fixnum and Bignum, it converts between the 2 automatically.

Yes that worked super.
21000000.00000001*1e8.to_i
>2100000000000001



Title: Re: Float value from bitcoind, how should I represent it?
Post by: Luke-Jr on April 12, 2011, 07:19:54 PM
round(1e8 * value) works in basically any language.