Author

Topic: Float value from bitcoind, how should I represent it? (Read 2820 times)

legendary
Activity: 2576
Merit: 1186
round(1e8 * value) works in basically any language.
hero member
Activity: 602
Merit: 513
GLBSE Support [email protected]
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

legendary
Activity: 1652
Merit: 2301
Chief Scientist
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.
hero member
Activity: 602
Merit: 513
GLBSE Support [email protected]
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.
administrator
Activity: 5222
Merit: 13032
After converting to decimal, make sure you round to 8 decimals of precision to fix any float errors.
member
Activity: 98
Merit: 13
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.

hero member
Activity: 602
Merit: 513
GLBSE Support [email protected]
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?
Jump to: