System.out.println(2e+5);
Above line's output is,
200000.0
While below line's,
System.out.println(2e-5);
output is,
2.0e-5
Why does 2e-5
is not solved down with -10^5
giving the output,
0.00002
System.out.println(2e+5);
Above line's output is,
200000.0
While below line's,
System.out.println(2e-5);
output is,
2.0e-5
Why does 2e-5
is not solved down with -10^5
giving the output,
0.00002
Because that's how the designers of the Double class chose to implement it:
[...]
- If m is greater than or equal to 10-3 but less than 107, then it is represented as the integer part of m, in decimal form with no leading zeroes, followed by '.' ('\u002E'), followed by one or more decimal digits representing the fractional part of m.
- If m is less than 10-3 or greater than or equal to 107, then it is represented in so-called "computerized scientific notation." [...]
They could have made another choice, of course, but that's simply how it is.
As the documentation says, if you want a specific format, use a NumberFormat
to format your double.
As already pointed out by @Nizet, when converting double to string, the output will have scientific notation if number of digits are large. And this is not just for decimals.
Consider:
double d = 1500000000;
System.out.println(String.valueOf(d));
This will print 1.5E9
If you want conversion to happen in readable format, use String.format
System.out.println(String.format ("%.0f", d));
will print 1500000000
© 2022 - 2024 — McMap. All rights reserved.
2e-5
is adouble
literal - it's64
ones and zeroes. There is no format, no notation, no decimal point. How its represented when printed is just that, a representation. It's meaningless. – Vagus2e
anything will always be zero... – Vagus1.23456e10
has the same problems. – Vagus