Bitcoin Forum
May 08, 2024, 08:51:51 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Float value from bitcoind, how should I represent it?  (Read 2792 times)
Nefario (OP)
Hero Member
*****
Offline Offline

Activity: 602
Merit: 512


GLBSE Support support@glbse.com


View Profile WWW
April 12, 2011, 05:17:53 AM
 #1

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?

PGP key id at pgp.mit.edu 0xA68F4B7C

To get help and support for GLBSE please email support@glbse.com
1715201511
Hero Member
*
Offline Offline

Posts: 1715201511

View Profile Personal Message (Offline)

Ignore
1715201511
Reply with quote  #2

1715201511
Report to moderator
"There should not be any signed int. If you've found a signed int somewhere, please tell me (within the next 25 years please) and I'll change it to unsigned int." -- Satoshi
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
xf2_org
Member
**
Offline Offline

Activity: 98
Merit: 13


View Profile
April 12, 2011, 06:52:47 AM
 #2

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.

theymos
Administrator
Legendary
*
Offline Offline

Activity: 5194
Merit: 12976


View Profile
April 12, 2011, 12:19:06 PM
 #3

After converting to decimal, make sure you round to 8 decimals of precision to fix any float errors.

1NXYoJ5xU91Jp83XfVMHwwTUyZFK64BoAD
Nefario (OP)
Hero Member
*****
Offline Offline

Activity: 602
Merit: 512


GLBSE Support support@glbse.com


View Profile WWW
April 12, 2011, 12:36:00 PM
 #4

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.

PGP key id at pgp.mit.edu 0xA68F4B7C

To get help and support for GLBSE please email support@glbse.com
Gavin Andresen
Legendary
*
qt
Offline Offline

Activity: 1652
Merit: 2216


Chief Scientist


View Profile WWW
April 12, 2011, 01:46:05 PM
 #5

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.

How often do you get the chance to work on a potentially world-changing project?
Nefario (OP)
Hero Member
*****
Offline Offline

Activity: 602
Merit: 512


GLBSE Support support@glbse.com


View Profile WWW
April 12, 2011, 01:55:10 PM
 #6

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


PGP key id at pgp.mit.edu 0xA68F4B7C

To get help and support for GLBSE please email support@glbse.com
Luke-Jr
Legendary
*
expert
Offline Offline

Activity: 2576
Merit: 1186



View Profile
April 12, 2011, 07:19:54 PM
 #7

round(1e8 * value) works in basically any language.

Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!