Search code examples
javabigdecimal

BigDecimal. multiply() and divide() methods return hexadecimal number. Why?


Here is my code:

public class Test1 {
    public static void main(String[] args) {
        BigDecimal wallet = new BigDecimal("0.0");
        BigDecimal productPrice = new BigDecimal("0.01");

        for (int i = 1; i <= 5; i++) {
            wallet = wallet.multiply(productPrice);
        }
        System.out.println(wallet);
    }
} 

Result: 0E-11
I have a question. Why am I getting the result in the hexadecimal number system and not in decimal? Like this: 2.45


Solution

  • In your case, the method toString is used which will use an exponent field if needed.

    You can use toPlainString if you do not want a string representation with an exponent field.

    System.out.println(wallet.toPlainString());