Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: xeryan on October 07, 2014, 01:40:21 PM



Title: how to handle btc in javascript
Post by: xeryan on October 07, 2014, 01:40:21 PM
If I have a string like "0.059" and I want to convert it to a number to do some operations like a division, can I in javascript simply convert it with parseFloat("0.059") or it could have some precisions problems, while executing a division for example ?

Secondly, how to correctly save and handle bitcoins values in php and mysql ?


Title: Re: how to handle btc in javascript
Post by: shorena on October 07, 2014, 01:50:59 PM
Internally bitcoin are handled as satoshi, thus there are no "float-problems". A division could still result in problems because e.g. 100k Satoshi / 3 would result in 333333 which might not be correct depending on your use case.


Title: Re: how to handle btc in javascript
Post by: xeryan on October 07, 2014, 01:56:50 PM
Internally bitcoin are handled as satoshi, thus there are no "float-problems". A division could still result in problems because e.g. 100k Satoshi / 3 would result in 333333 which might not be correct depending on your use case.
And so, how can be resolved this issue ? Thank you


Title: Re: how to handle btc in javascript
Post by: s2 on October 07, 2014, 03:09:51 PM
Multiply it up by 100,000,000 and use integers to just count satoshis.  If you're handling > 10 BTC you'll want to check that it's using 64bit integers or equivalent.

In Javascript everything is floating point and I believe you get 53 bits of integer range so you'll safely handle 90,071,992 bitcoins (52bits) which should be more than enough for the 21 million out there.

alert([Number.MAX_VALUE, Number.MIN_VALUE]);


Title: Re: how to handle btc in javascript
Post by: xeryan on January 23, 2015, 01:09:29 PM
Seems like parseFloat is good:

parseFloat("15854420.40053001")

Outputs the correct value and then I can multiply this value for 100,000,000 to make operations, right?


Title: Re: how to handle btc in javascript
Post by: jxm262 on January 23, 2015, 07:47:06 PM
I know this is kind of something you can google yourself (or probably have), but here's a couple SO links with some more info.

http://stackoverflow.com/questions/1458633/elegant-workaround-for-javascript-floating-point-number-problem

http://stackoverflow.com/questions/2876536/precise-financial-calculation-in-javascript-what-are-the-gotchas

http://floating-point-gui.de/languages/javascript/


FWIW, I typically use parseFloat, then toFixed(2) when trying to convert to currency format.  Of course this depends on the precision required though.

Example:  parseFloat("23432.234324").toFixed(2);  //"23432.23"


Title: Re: how to handle btc in javascript
Post by: jxm262 on January 23, 2015, 07:53:13 PM
Update:  for your above..

parseFloat("15854420.40053001").toFixed(8);


Title: Re: how to handle btc in javascript
Post by: coinableS on January 24, 2015, 04:44:17 PM
Update:  for your above..

parseFloat("15854420.40053001").toFixed(8);

 :D your toFixed8 got converted to a smiley.  I've used both methods, the float and just integer and count satoshis. I like the satoshi approach more when storing in my DB IMO.


Title: Re: how to handle btc in javascript
Post by: coinpr0n on January 25, 2015, 08:59:42 PM
Update:  for your above..

parseFloat("15854420.40053001").toFixed(8);

 :D your toFixed8 got converted to a smiley.  I've used both methods, the float and just integer and count satoshis. I like the satoshi approach more when storing in my DB IMO.

I agree. Usually use integers too. And I think I remember blockchain.info's API also works that way.


Title: Re: how to handle btc in javascript
Post by: true-asset on March 09, 2015, 10:56:14 AM
Using integers is the correct way. You only use floats if you don;t know how many decimal places there are.


Title: Re: how to handle btc in javascript
Post by: emrebey on March 09, 2015, 11:24:00 AM
maybe this helps: https://en.bitcoin.it/wiki/Proper_Money_Handling_(JSON-RPC)

not directly related to javascript but  I store as decimal ( 16, 8 ) on MySQL


Title: Re: how to handle btc in javascript
Post by: coinpr0n on March 09, 2015, 12:52:48 PM
maybe this helps: https://en.bitcoin.it/wiki/Proper_Money_Handling_(JSON-RPC)

not directly related to javascript but  I store as decimal ( 16, 8 ) on MySQL

Also, consider using one of the Bitcoin javascript frameworks. Bitcore (http://bitcore.io/) is pretty cool, works both server-side and client-side.


Title: Re: how to handle btc in javascript
Post by: coinableS on March 09, 2015, 06:14:12 PM
This is an old thread people! Issue was solved...  ::)