## avoid rounding errors in javascript with bigdecimal?

1

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 – Mikko Ohtamaa – 2014-10-29T13:12:12.577

on stackoverflow nobody answered my 6month post. thats why i'm here, but it seems like nobody is interested in helping. – harry – 2014-10-29T13:13:49.090

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)

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 – 2014-10-29T13:41:54.077

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 – 2014-11-01T04:52:03.237

okay, but i use HALF_EVEN 3 times. do i need the last one? – harry – 2014-11-03T07:29:53.220