Bitcoin Forum
December 08, 2016, 04:28:52 AM *
News: Latest stable version of Bitcoin Core: 0.13.1  [Torrent].
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: Float value from bitcoind, how should I represent it?  (Read 2499 times)
Nefario
Hero Member
*****
Offline Offline

Activity: 602


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
1481171332
Hero Member
*
Offline Offline

Posts: 1481171332

View Profile Personal Message (Offline)

Ignore
1481171332
Reply with quote  #2

1481171332
Report to moderator
1481171332
Hero Member
*
Offline Offline

Posts: 1481171332

View Profile Personal Message (Offline)

Ignore
1481171332
Reply with quote  #2

1481171332
Report to moderator
1481171332
Hero Member
*
Offline Offline

Posts: 1481171332

View Profile Personal Message (Offline)

Ignore
1481171332
Reply with quote  #2

1481171332
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1481171332
Hero Member
*
Offline Offline

Posts: 1481171332

View Profile Personal Message (Offline)

Ignore
1481171332
Reply with quote  #2

1481171332
Report to moderator
xf2_org
Member
**
Offline Offline

Activity: 70


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
*
expert
Offline Offline

Activity: 2492


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
Hero Member
*****
Offline Offline

Activity: 602


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


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
Hero Member
*****
Offline Offline

Activity: 602


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: 2086



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

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

Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!