<p>Good idea on storing it in Satoshis. With anything involving money, you want to avoid floats due to rounding errors. The best way of doing this would be to parse the value in Satoshis as a string, then simply put a decimal place 8 digits from the end to get the value in Satoshis. </p>

<pre><code>function convertToBTC(satoshis){
    var satoshi_string = satoshis.toString();
    var len_satoshi   = satoshi_string.length;
    if (len_satoshi &lt; 9){
        concat_string = concat_zeroes(9 - len_satoshi, satoshi_string);
     }
    var btc_len = concat_string.length;
    var decimal_place = btc_len-8;
    var bitcoin_string = concat_string.slice(0,decimal_place).concat('.').concat(concat_string.slice(decimal_place, btc_len));
    return bitcoin_string;
}

function concat_zeroes(num_zeroes, string1){
    while(num_zeroes){
         string1 = '0'.concat(string1);
         num_zeroes -= 1;
    }
    return string1;
}
</code></pre>

CC BY-SA 3.0
2015-01-17T05:38:28.977Z
2015-01-17T05:38:28.977Z
jdb6167
<p>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. </p>

<p><strong>Here's what I've tried:</strong></p>

<p>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. </p>

<pre><code>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);
    }

};
</code></pre>

CC BY-SA 3.0
2015-01-04T04:26:28.667Z
You 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
CC BY-SA 3.0
2015-01-04T22:56:21.637Z
@StephenM347 ok, i've tried to improve the question.
r3wt
<javascript><precision>
Prevent rounding errors when displaying bitcoin values in javascript
r3wt
# Prevent rounding errors when displaying bitcoin values in javascript

(Asked by: username_0 on 2015-01-04)

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);
    }

};

```

**Tags:** javascript, precision

> **Comments:**
> **username_1**: You should probably try to rephrase your question so that it is more generally applicable. Bitcoin SE is more suited towards learning than code review...  
> **username_0**: @StephenM347 ok, i've tried to improve the question.  

---

## 1 answers

### Answer by username_2 on 2015-01-17. Score: 3

Good idea on storing it in Satoshis. With anything involving money, you want to avoid floats due to rounding errors. The best way of doing this would be to parse the value in Satoshis as a string, then simply put a decimal place 8 digits from the end to get the value in Satoshis.

```
function convertToBTC(satoshis){
    var satoshi_string = satoshis.toString();
    var len_satoshi   = satoshi_string.length;
    if (len_satoshi < 9){
        concat_string = concat_zeroes(9 - len_satoshi, satoshi_string);
     }
    var btc_len = concat_string.length;
    var decimal_place = btc_len-8;
    var bitcoin_string = concat_string.slice(0,decimal_place).concat('.').concat(concat_string.slice(decimal_place, btc_len));
    return bitcoin_string;
}

function concat_zeroes(num_zeroes, string1){
    while(num_zeroes){
         string1 = '0'.concat(string1);
         num_zeroes -= 1;
    }
    return string1;
}

```


---

