Double-write the whole number

How can I get double in Java to print the whole number. Not like 1.65462165887E12. But how is 1654621658874684?

thanks

+4
source share
4 answers

Format it accordingly. For instance:

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

Or you can use it as a String:

 //"%.1f" this mean, how many number after the comma String value = String.format("%.1f", 1654621658874684.0); 

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

+9
source

You can use String.format() :

 System.out.println(String.format("%.0f", 1654621658874684.0d)); // prints 1654621658874684 
+2
source

String formatted = String.format("%f", dblNumber);

0
source

I use something like

 if((long) d == d) System.out.println((long) d); else System.out.println(d); 

long can have 18 digits, while double has at best 15-16 digits of precision.

0
source

Source: https://habr.com/ru/post/1400216/


All Articles