0

I have a very weird result when I'm using my function and I think that I'm missing something with the rounding and double in java.

For example, when I provide the value 00159,300 for number and 100 for conversion I have 15930,000000000002 which is not possible!

public static String convertMultiply(String number, String conversion) {
    number=number.replace(",", ".");
    BigDecimal res=BigDecimal.valueOf(Double.valueOf(number)*Integer.valueOf(conversion));
    res=res.stripTrailingZeros();
    return res.toPlainString().replace(".", ",");
}

thanks in advance!

soufiane
  • 21
  • 6

1 Answers1

5

Double is an approximation of decimal values in Java. Instead, replace your line using double with:

BigDecimal res = (new BigDecimal(number)).multiply(new BigDecimal(conversion));
Kevin Hooke
  • 2,583
  • 2
  • 19
  • 33