Author

Topic: how to handle btc in javascript (Read 3767 times)

legendary
Activity: 1442
Merit: 1186
March 09, 2015, 01:14:12 PM
#13
This is an old thread people! Issue was solved...  Roll Eyes
hero member
Activity: 910
Merit: 1000
March 09, 2015, 07:52:48 AM
#12
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 is pretty cool, works both server-side and client-side.
sr. member
Activity: 476
Merit: 250
March 09, 2015, 06:24:00 AM
#11
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
sr. member
Activity: 252
Merit: 250
Uro: 1 URO = 1 metric tonne of Urea N46 fertilizer
March 09, 2015, 05:56:14 AM
#10
Using integers is the correct way. You only use floats if you don;t know how many decimal places there are.
hero member
Activity: 910
Merit: 1000
January 25, 2015, 03:59:42 PM
#9
Update:  for your above..

parseFloat("15854420.40053001").toFixed(Cool;

 Cheesy 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.
legendary
Activity: 1442
Merit: 1186
January 24, 2015, 11:44:17 AM
#8
Update:  for your above..

parseFloat("15854420.40053001").toFixed(Cool;

 Cheesy 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.
newbie
Activity: 18
Merit: 0
January 23, 2015, 02:53:13 PM
#7
Update:  for your above..

parseFloat("15854420.40053001").toFixed(Cool;
newbie
Activity: 18
Merit: 0
January 23, 2015, 02:47:06 PM
#6
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"
sr. member
Activity: 337
Merit: 250
HTML5/Node.js/PHP developer
January 23, 2015, 08:09:29 AM
#5
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?
s2
full member
Activity: 198
Merit: 123
October 07, 2014, 10:09:51 AM
#4
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]);
sr. member
Activity: 337
Merit: 250
HTML5/Node.js/PHP developer
October 07, 2014, 08:56:50 AM
#3
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
copper member
Activity: 1498
Merit: 1528
No I dont escrow anymore.
October 07, 2014, 08:50:59 AM
#2
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.
sr. member
Activity: 337
Merit: 250
HTML5/Node.js/PHP developer
October 07, 2014, 08:40:21 AM
#1
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 ?
Jump to: