How can I stop Java from cutting a large double?

Suppose we have the following code:

System.out.println(String.valueOf(100000000000.0));

Now the output will be 1.0E11. But that is not what I want. (It looks bad at a record level) I want it to produce exactly 100000000000.0. Is there any way to do this?

+3
source share
4 answers

Format it accordingly. For instance:

System.out.printf("%.1f", 1654621658874684.0);

Remember that double is not infinitely accurate. It has an accuracy of 15 to 17 decimal digits. If you want arbitrary precision floating point numbers, use BigDecimal instead of double.

Or you can use String.format ():

System.out.println(String.format("%.0f", 1654621658874684.0d));
+6
source

System.out.printf("Score: %.0f\n", 1e5);will print 100000.

+2
source

... Quest
DecimalFormat

0
source

For such large numbers, I think you should use BigDecimal.

0
source

All Articles