How can I force a double
in Java to print the whole number. Not like 1.65462165887E12.But like 1654621658874684?
Thanks
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With