Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does +e canceled and -e not?

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
like image 898
Roshana Pitigala Avatar asked Jul 20 '18 16:07

Roshana Pitigala


1 Answers

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.

like image 61
JB Nizet Avatar answered Sep 22 '22 08:09

JB Nizet