xeryan (OP)
Sr. Member
Offline
Activity: 337
Merit: 250
HTML5/Node.js/PHP developer
|
|
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 ?
|
|
|
|
shorena
Copper Member
Legendary
Offline
Activity: 1498
Merit: 1540
No I dont escrow anymore.
|
|
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.
|
Im not really here, its just your imagination.
|
|
|
xeryan (OP)
Sr. Member
Offline
Activity: 337
Merit: 250
HTML5/Node.js/PHP developer
|
|
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
|
|
|
|
s2
|
|
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]);
|
|
|
|
xeryan (OP)
Sr. Member
Offline
Activity: 337
Merit: 250
HTML5/Node.js/PHP developer
|
|
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?
|
|
|
|
|
jxm262
Newbie
Offline
Activity: 18
Merit: 0
|
|
January 23, 2015, 07:53:13 PM |
|
Update: for your above.. parseFloat("15854420.40053001").toFixed( ;
|
|
|
|
coinableS
Legendary
Offline
Activity: 1442
Merit: 1186
|
|
January 24, 2015, 04:44:17 PM |
|
Update: for your above.. parseFloat("15854420.40053001").toFixed( ; 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
|
|
January 25, 2015, 08:59:42 PM |
|
Update: for your above.. parseFloat("15854420.40053001").toFixed( ; 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
|
|
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.
|
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.
|
|
|
|
coinpr0n
|
|
March 09, 2015, 12:52:48 PM |
|
Also, consider using one of the Bitcoin javascript frameworks. Bitcore is pretty cool, works both server-side and client-side.
|
|
|
|
coinableS
Legendary
Offline
Activity: 1442
Merit: 1186
|
|
March 09, 2015, 06:14:12 PM |
|
This is an old thread people! Issue was solved...
|
|
|
|
|