Search code examples
javadoublewrapper

How to output the Double with 20 decimal places?


Hello to all,

Double d = 1.000000000000000000000000000000001;
        System.out.println(d);

The code above prints 1.0 but i want d to store entire value. desired output was 1.000000000000000000000000000000001

my concern is how to store this value in double? is there any way to increase the decimals a double can store? please guide me


Solution

  • You need to use BigDecimal.

    Example:

    BigDecimal test = new BigDecimal("1.000000000000000000000000000000001");
    System.out.println(test);
    

    The reason that the same cannot be represented using a double is that double has 64 bits.