0
    String pattern = "0.00";
    double number = 999999999999999999999d;
    DecimalFormat decimalFormat = new DecimalFormat(pattern);
    decimalFormat.applyPattern(pattern);

    System.out.println(decimalFormat.format(number));

prints 1000000000000000000000.00

How can I print actual value.In this case I expect 999999999999999999999.00

  • A lot has been written on this... http://stackoverflow.com/questions/2100490/floating-point-inaccuracy-examples , http://stackoverflow.com/questions/5257166/java-floats-and-doubles-how-to-avoid-that-0-0-0-1-0-1-0-9000001 , http://stackoverflow.com/questions/5423545/sum-of-decimal-number-in-java – Danielson Jul 07 '15 at 10:05
  • practically do you use such a large number? – SpringLearner Jul 07 '15 at 10:10
  • Possible duplicate: http://stackoverflow.com/questions/22028675/using-decimalformat-to-format-big-double-numbers – assylias Jul 07 '15 at 10:12

2 Answers2

0

You can use BigDecimal to handle large number.

The BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. The toString() method provides a canonical representation of a BigDecimal.

Garry
  • 4,493
  • 3
  • 28
  • 48
0

It's not a DecimalFormat issue - 999999999999999999999d really does equal 1000000000000000000000. If you want to store very large integers like this one, you should use the BigInteger class - a double only has enough precision to store about 15-16 significant figures accurately.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110