Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: init1 on October 26, 2015, 11:43:53 AM



Title: fractional amount in java
Post by: init1 on October 26, 2015, 11:43:53 AM
Hello!

Which type for amount coins more appropriate in Java?

For example have amount like 0.00001. When use for example double it transforms to '1e-005'
float 9.99999974737875e-006 ..

Any special / custom  types ?

How to calculate amount in the correct way ?


Thanks


Title: Re: fractional amount in java
Post by: achow101 on October 26, 2015, 11:45:35 AM
Use a long with the amount in satoshi. Then when you need to convert to display an amount, just divide by 100000000


Title: Re: fractional amount in java
Post by: Foxpup on October 26, 2015, 01:20:03 PM
What altcoin are you using where 0.00000999999974737875 is different from 0.00001? Bitcoin only uses 8 decimal places, which means double precision is good enough for any value up to 67,108,863.99999999, which is more than the number of bitcoins that will ever exist. This is likely intentional on Satoshi's part.


Title: Re: fractional amount in java
Post by: achow101 on October 26, 2015, 01:34:47 PM
What altcoin are you using where 0.00000999999974737875 is different from 0.00001? Bitcoin only uses 8 decimal places, which means double precision is good enough for any value up to 67,108,863.99999999, which is more than the number of bitcoins that will ever exist. This is likely intentional on Satoshi's part.
Pretty sure that it is just weirdness on java, not that he needs that many decimal places of precision.


Title: Re: fractional amount in java
Post by: OnkelPaul on October 26, 2015, 01:43:58 PM
Use a long with the amount in satoshi. Then when you need to convert to display an amount, just divide by 100000000

Wot he sez.

When you're dealing with money, forget about floating point, instead use fixed point numbers with the right number of decimals if your programming language has them, or just integer numbers in the smallest unit that you want to deal with (then you need to remember to insert a decimal point when talking to the outside world.)

Binary floating point cannot represent decimal fractional numbers exactly. This is not a fault in the programming language or its libraries, it's just a natural limitation when using fractional numbers and switching between different bases.

Onkel Paul


Title: Re: fractional amount in java
Post by: hendo420 on October 27, 2015, 08:04:12 PM
In the bitcoin code a singular unit of measure is 1 satoshi. Its actually defined COIN and CENT ect.

So 1 Coin is 100,000,000 satoshi but it shows as 1 in the client. No need to deal with decimals.

Just do something similar.


Title: Re: fractional amount in java
Post by: dexX7 on October 28, 2015, 01:44:47 PM
You may use double-precision + round to nearest 0.00000001 (which should yield accurate results), but I'd recommend to use plain integers, which is probably less error prone.

See also:

https://en.bitcoin.it/wiki/Proper_Money_Handling_(JSON-RPC)


Title: Re: fractional amount in java
Post by: Sampey on October 29, 2015, 06:15:05 PM
Use BIGDECIMAL class http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial

I use that in my Bot and never got problems ;)


Title: Re: fractional amount in java
Post by: teukon on October 30, 2015, 06:23:42 AM
Which type for amount coins more appropriate in Java?

Ideally you'd use an integer type, preferrably unsigned.  I recommend following the good example set by the reference client by working entirely in satoshi (BTC, mBTC, and µBTC are used only for human input/output).

I'd only resort to double precision floating types when true integers are not readily available (e.g. javascript).