# avoid rounding errors in javascript with bigdecimal?

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!

• You might get more lucky with this programming specific question on stackoverflow.com Oct 29 '14 at 13:12
• on stackoverflow nobody answered my 6month post. thats why i'm here, but it seems like nobody is interested in helping. Oct 29 '14 at 13:13

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)

• 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? Oct 29 '14 at 13:41
• 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. Nov 1 '14 at 4:52
• okay, but i use HALF_EVEN 3 times. do i need the last one? Nov 3 '14 at 7:29