Author

Topic: fractional amount in java (Read 1274 times)

legendary
Activity: 1246
Merit: 1011
October 30, 2015, 01:23:42 AM
#9
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).
legendary
Activity: 2632
Merit: 1040
October 29, 2015, 01:15:05 PM
#8
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 Wink
legendary
Activity: 1106
Merit: 1026
October 28, 2015, 08:44:47 AM
#7
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)
sr. member
Activity: 420
Merit: 250
October 27, 2015, 03:04:12 PM
#6
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.
legendary
Activity: 1039
Merit: 1005
October 26, 2015, 08:43:58 AM
#5
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
staff
Activity: 3458
Merit: 6793
Just writing some code
October 26, 2015, 08:34:47 AM
#4
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.
legendary
Activity: 4536
Merit: 3188
Vile Vixen and Miss Bitcointalk 2021-2023
October 26, 2015, 08:20:03 AM
#3
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.
staff
Activity: 3458
Merit: 6793
Just writing some code
October 26, 2015, 06:45:35 AM
#2
Use a long with the amount in satoshi. Then when you need to convert to display an amount, just divide by 100000000
jr. member
Activity: 43
Merit: 7
October 26, 2015, 06:43:53 AM
#1
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
Jump to: