Convert very small double values to string (with scientific notation) (Java)
-
09-10-2019 - |
Question
I'm trying to print a small double number like 6.67e-11
, but using Double.toString() returns 0
. What can I do to make it print 6.67e-11 (or something similar) instead?
Solution
Unable to reproduce:
public class Test {
public static void main(String args[])
{
double d = 6.67e-11;
System.out.println(Double.toString(d)); // Prints "6.67E-11"
}
}
IIRC, Double.toString()
always returns a string which allows the exact value to be round-tripped using Double.parseDouble()
.
My guess is that you don't actually have a small value - that you have 0, due to some rounding errors in other operations.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow