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


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

output: 359756800

eur / smallest unit:

var eur_b = eur_s.divide(satoshi, 8, mode);

output: 3.597568

but since eur has only 2 decimals:


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 Commented Oct 29, 2014 at 13:12
  • on stackoverflow nobody answered my 6month post. thats why i'm here, but it seems like nobody is interested in helping.
    – harry
    Commented Oct 29, 2014 at 13:13

1 Answer 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:


output: 3.60


  • 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
    Commented Oct 29, 2014 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.
    – Seventoes
    Commented Nov 1, 2014 at 4:52
  • okay, but i use HALF_EVEN 3 times. do i need the last one?
    – harry
    Commented Nov 3, 2014 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.