<p>This is not a rounding problem.  The 3.6 EUR is correctly rounded to two decimals from 3.597568 EUR.  </p>

<p>Computers don't know when we want to see trailing zeros.  The problem is formatting:</p>

<pre><code>console.log(eur_b.setScale(2,mode).longValue().toFixed(2));

output: 3.60
</code></pre>

<p>(<a href="http://jsfiddle.net/x90f3p5x/" rel="nofollow">fiddle</a>)</p>

CC BY-SA 3.0
2014-10-29T13:41:54.077Z
my question was if the calculations before i scale it to 2 decimals are correct. i want to know if the user must pay 3.6 or 3.59?
harry
CC BY-SA 3.0
2014-11-01T04:52:03.237Z
The math is right, how you handle rounding differences will be entirely up to you. If you use HALF_EVEN the rounding should be fair to the users, on average.
Seventoes
CC BY-SA 3.0
2014-11-03T07:29:53.220Z
okay, but i use HALF_EVEN 3 times. do i need the last one?
harry
CC BY-SA 3.0
2014-10-29T13:40:10.767Z
2014-10-29T13:40:10.767Z
Joe Flynn
<p>a user submits a buy request: 0.0104 BTC at a rate of 345.92 EUR</p>

<p>config:</p>

<pre><code>var mode = RoundingMode.HALF_EVEN();
var satoshi = new BigDecimal("100000000");
</code></pre>

<p>user input:</p>

<pre><code>var btc_b = new BigDecimal("0.0104");
var rate_b = new BigDecimal("345.92");
</code></pre>

<p>convert amount to smallest unit:</p>

<pre><code>var btc_s = btc_b.multiply(satoshi);
</code></pre>

<p>btc * rate = spend:</p>

<pre><code>var eur_s = btc_s.multiply(rate_b).setScale(0,mode);
console.log(eur_s.longValue());

output: 359756800
</code></pre>

<p>eur / smallest unit:</p>

<pre><code>var eur_b = eur_s.divide(satoshi, 8, mode);
console.log(eur_b.longValue());

output: 3.597568
</code></pre>

<p>but since eur has only 2 decimals:</p>

<pre><code>console.log(eur_b.setScale(2,mode).longValue());

output: 3.6
</code></pre>

<p>i assume the user must spend 3.6 EUR for 0.0104 BTC at a rate of 345.92 EUR</p>

<p>here's a fiddle: jsfiddle.net/nvkja98c/</p>

<p>please correct me if i'm wrong!</p>

CC BY-SA 3.0
2014-10-29T13:12:12.577Z
You might get more lucky with this programming specific question on stackoverflow.com
Mikko Ohtamaa
CC BY-SA 3.0
2014-10-29T13:13:49.090Z
on stackoverflow nobody answered my 6month post. thats why i'm here, but it seems like nobody is interested in helping.
harry
<currencies><javascript>
avoid rounding errors in javascript with bigdecimal?
harry
# avoid rounding errors in javascript with bigdecimal?

(Asked by: username_0 on 2014-10-29)

a user submits a buy request: 0.0104 BTC at a rate of 345.92 EUR

config:

```
var mode = RoundingMode.HALF_EVEN();
var satoshi = new BigDecimal("100000000");

```

user input:

```
var btc_b = new BigDecimal("0.0104");
var rate_b = new BigDecimal("345.92");

```

convert amount to smallest unit:

```
var btc_s = btc_b.multiply(satoshi);

```

btc \* rate = spend:

```
var eur_s = btc_s.multiply(rate_b).setScale(0,mode);
console.log(eur_s.longValue());

output: 359756800

```

eur / smallest unit:

```
var eur_b = eur_s.divide(satoshi, 8, mode);
console.log(eur_b.longValue());

output: 3.597568

```

but since eur has only 2 decimals:

```
console.log(eur_b.setScale(2,mode).longValue());

output: 3.6

```

i assume the user must spend 3.6 EUR for 0.0104 BTC at a rate of 345.92 EUR

here's a fiddle: jsfiddle.net/nvkja98c/

please correct me if i'm wrong!

**Tags:** currencies, javascript

> **Comments:**
> **username_1**: You might get more lucky with this programming specific question on stackoverflow.com  
> **username_0**: on stackoverflow nobody answered my 6month post. thats why i'm here, but it seems like nobody is interested in helping.  

---

## 1 answers

### Answer by username_2 on 2014-10-29. Score: 1

This is not a rounding problem. The 3.6 EUR is correctly rounded to two decimals from 3.597568 EUR.

Computers don't know when we want to see trailing zeros. The problem is formatting:

```
console.log(eur_b.setScale(2,mode).longValue().toFixed(2));

output: 3.60

```

([fiddle](http://jsfiddle.net/x90f3p5x/))

> **Comments:**
> **username_0**: my question was if the calculations before i scale it to 2 decimals are correct. i want to know if the user must pay 3.6 or 3.59?  
> **username_3**: The math is right, how you handle rounding differences will be entirely up to you. If you use HALF_EVEN the rounding should be fair to the users, on average.  
> **username_0**: okay, but i use HALF_EVEN 3 times. do i need the last one?  

---

