Search code examples
javabigdecimalrounding

Problems with BigDecimal.ROUND_UP


Why I am getting 184.84 as sell rate? Why is it acting like this in BigDecimal.ROUND_UP. I checked with the BigDecimal.ROUND_HALF_EVEN. It works fine. But I wanna know why is it acting like this.

for(int i = 0; i < 50; i++){
    double  sellrate    = 184.83;
    BigDecimal sellRate = new BigDecimal(sellrate);
    sellRate    = sellRate.setScale(2,BigDecimal.ROUND_UP);
    System.out.println("sellRate : "+sellRate); 
}

Solution

  • This is why:

    double sellrate = 184.83;
    BigDecimal sellRate = new BigDecimal(sellrate);
    System.out.println("sellRate: " + sellRate);
    

    prints

    sellRate: 184.830000000000012505552149377763271331787109375
    

    This is explained in the BigDecimal JavaDoc:

    The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the value that is being passed in to the constructor is not exactly equal to 0.1, appearances notwithstanding.

    Use the String constructor

    BigDecimal sellRate = new BigDecimal("184.83");
    

    if you want to get the exact value for your BigDecimal.