为什么 +e 取消而 -e 不取消?

Why does +e canceled and -e not?

System.out.println(2e+5);

上面一行的输出是,

200000.0

线下时,

System.out.println(2e-5);

输出是,

2.0e-5

为什么 2e-5 没有用 -10^5 给出输出解决,

0.00002

因为 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." [...]

当然,他们本可以做出其他选择,但事实就是如此。

如文档所述,如果您需要特定格式,请使用 NumberFormat 来格式化您的双精度格式。

正如@Nizet 已经指出的那样,将double 转换为string 时,如果位数很大,输出将具有科学计数法。这不仅适用于小数。

考虑:

double d = 1500000000;
System.out.println(String.valueOf(d));

这将打印 1.5E9

如果您希望以可读格式进行转换,请使用 String.format

System.out.println(String.format ("%.0f", d));

将打印 1500000000