5
What is the correct way to prevent Floating Point Precision errors when displaying bitcon values in javascript? I've seen many answers on the topic, but they all seem to have their drawbacks.
Here's what I've tried:
I've chosen to store bitcoin values in satoshis, then simply convert to float on the front end using simple math. Is this correct? I'm trying to avoid float precision errors. It seems to work as intended when testing with a calculator as reference, but I would love to know for sure.
var units = {
// In satoshis
coin: 100000000,
format_to: function(val) {
// 1 BTC = 10^8 Satoshis
return (val / units.coin).toFixed(8);
},
format_from: function(val) {
// Parse the string as float the multiply * coin.
return (parseFloat(val) * units.coin);
}
};
1Where do you retrieve the bitcoin values from? Are they being represented in satoshis value at their source? – George Kimionis – 2015-01-04T04:23:21.813
1You should probably try to rephrase your question so that it is more generally applicable. Bitcoin SE is more suited towards learning than code review... – morsecoder – 2015-01-04T04:26:28.667
@StephenM347 ok, i've tried to improve the question. – r3wt – 2015-01-04T22:56:21.637