Bitcoin Forum
May 05, 2024, 03:49:30 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: fractional amount in java  (Read 1248 times)
init1 (OP)
Newbie
*
Offline Offline

Activity: 43
Merit: 0


View Profile
October 26, 2015, 11: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
Bitcoin mining is now a specialized and very risky industry, just like gold mining. Amateur miners are unlikely to make much money, and may even lose money. Bitcoin is much more than just mining, though!
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1714924170
Hero Member
*
Offline Offline

Posts: 1714924170

View Profile Personal Message (Offline)

Ignore
1714924170
Reply with quote  #2

1714924170
Report to moderator
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3388
Merit: 6581


Just writing some code


View Profile WWW
October 26, 2015, 11: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

Foxpup
Legendary
*
Offline Offline

Activity: 4354
Merit: 3042


Vile Vixen and Miss Bitcointalk 2021-2023


View Profile
October 26, 2015, 01:20:03 PM
 #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.

Will pretend to do unspeakable things (while actually eating a taco) for bitcoins: 1K6d1EviQKX3SVKjPYmJGyWBb1avbmCFM4
I am not on the scammers' paradise known as Telegram! Do not believe anyone claiming to be me off-forum without a signed message from the above address! Accept no excuses and make no exceptions!
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3388
Merit: 6581


Just writing some code


View Profile WWW
October 26, 2015, 01:34:47 PM
 #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.

OnkelPaul
Legendary
*
Offline Offline

Activity: 1039
Merit: 1003



View Profile
October 26, 2015, 01:43:58 PM
 #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

hendo420
Sr. Member
****
Offline Offline

Activity: 420
Merit: 250



View Profile WWW
October 27, 2015, 08: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.

ebmarket.co
dexX7
Legendary
*
Offline Offline

Activity: 1106
Merit: 1024



View Profile WWW
October 28, 2015, 01:44:47 PM
 #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)

Sampey
Legendary
*
Offline Offline

Activity: 2632
Merit: 1040



View Profile
October 29, 2015, 06: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
teukon
Legendary
*
Offline Offline

Activity: 1246
Merit: 1002



View Profile
October 30, 2015, 06: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).
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!