Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force a double to write the whole Number

Tags:

java

double

How can I force a double in Java to print the whole number. Not like 1.65462165887E12.But like 1654621658874684?

Thanks

like image 939
Lepo Avatar asked Mar 07 '12 13:03

Lepo


People also ask

How do you round double to int?

round() Math. round() accepts a double value and converts it into the nearest long value by adding 0.5 to the value and truncating its decimal points. The long value can then be converted to an int using typecasting.

How do you convert double to long?

Let's check a straightforward way to cast the double to long using the cast operator: Assert. assertEquals(9999, (long) 9999.999); Applying the (long) cast operator on a double value 9999.999 results in 9999.


1 Answers

Format it appropriately. For example:

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

Or you can use it like a String:

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

Be aware that double is not infinitely precise. It has a precision of about 15 to 17 decimal digits. If you need floating-point numbers with arbitrary precision, use BigDecimal instead of double.

like image 165
Jesper Avatar answered Nov 03 '22 00:11

Jesper