Bitcoin Forum
April 30, 2024, 05:19:56 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: how to handle btc in javascript  (Read 3741 times)
xeryan (OP)
Sr. Member
****
Offline Offline

Activity: 337
Merit: 250

HTML5/Node.js/PHP developer


View Profile
October 07, 2014, 01:40:21 PM
 #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 ?
1714497596
Hero Member
*
Offline Offline

Posts: 1714497596

View Profile Personal Message (Offline)

Ignore
1714497596
Reply with quote  #2

1714497596
Report to moderator
"There should not be any signed int. If you've found a signed int somewhere, please tell me (within the next 25 years please) and I'll change it to unsigned int." -- Satoshi
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1714497596
Hero Member
*
Offline Offline

Posts: 1714497596

View Profile Personal Message (Offline)

Ignore
1714497596
Reply with quote  #2

1714497596
Report to moderator
1714497596
Hero Member
*
Offline Offline

Posts: 1714497596

View Profile Personal Message (Offline)

Ignore
1714497596
Reply with quote  #2

1714497596
Report to moderator
shorena
Copper Member
Legendary
*
Offline Offline

Activity: 1498
Merit: 1499


No I dont escrow anymore.


View Profile WWW
October 07, 2014, 01:50:59 PM
 #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.

Im not really here, its just your imagination.
xeryan (OP)
Sr. Member
****
Offline Offline

Activity: 337
Merit: 250

HTML5/Node.js/PHP developer


View Profile
October 07, 2014, 01:56:50 PM
 #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
s2
Full Member
***
Offline Offline

Activity: 198
Merit: 123


View Profile
October 07, 2014, 03:09:51 PM
 #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]);
xeryan (OP)
Sr. Member
****
Offline Offline

Activity: 337
Merit: 250

HTML5/Node.js/PHP developer


View Profile
January 23, 2015, 01:09:29 PM
 #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?
jxm262
Newbie
*
Offline Offline

Activity: 18
Merit: 0


View Profile WWW
January 23, 2015, 07: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"
jxm262
Newbie
*
Offline Offline

Activity: 18
Merit: 0


View Profile WWW
January 23, 2015, 07:53:13 PM
 #7

Update:  for your above..

parseFloat("15854420.40053001").toFixed(Cool;
coinableS
Legendary
*
Offline Offline

Activity: 1442
Merit: 1179



View Profile WWW
January 24, 2015, 04:44:17 PM
 #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.

coinpr0n
Hero Member
*****
Offline Offline

Activity: 910
Merit: 1000



View Profile
January 25, 2015, 08: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.

true-asset
Sr. Member
****
Offline Offline

Activity: 252
Merit: 250

Uro: 1 URO = 1 metric tonne of Urea N46 fertilizer


View Profile WWW
March 09, 2015, 10: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.

Uro: A Real Long Term Currency, 1 URO = 1 metric tonne of Urea N46 fertilizer[/url]
Urea N46 tracks gradual increases in energy and food prices over the long term.
emrebey
Sr. Member
****
Offline Offline

Activity: 476
Merit: 250


View Profile
March 09, 2015, 11: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
coinpr0n
Hero Member
*****
Offline Offline

Activity: 910
Merit: 1000



View Profile
March 09, 2015, 12:52:48 PM
 #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.

coinableS
Legendary
*
Offline Offline

Activity: 1442
Merit: 1179



View Profile WWW
March 09, 2015, 06:14:12 PM
 #13

This is an old thread people! Issue was solved...  Roll Eyes

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!