java - 为什么输出是科学计数法?

标签 java string decimal bigdecimal scientific-notation

为什么下面代码输出的是科学计数法?

      BigDecimal val = new BigDecimal("0000.000000111");
      System.out.println(val);

输出:1.11e-8

但是这段代码的输出是正确的十进制格式。

      BigDecimal val = new BigDecimal("0000.000011111");
      System.out.println(val);

输出:0.000011111

有没有办法为第一个字符串(如第二个输出)获取正确的十进制值?

最佳答案

如果你看the toString() function of BigDecimal ,它解释了如何创建输出:

Next, an adjusted exponent is calculated; this is the negated scale, plus the number of characters in the converted unscaled value, less one. That is, -scale+(ulength-1), where ulength is the length of the absolute value of the unscaled value in decimal digits (its precision).

If the scale is greater than or equal to zero and the adjusted exponent is greater than or equal to -6, the number will be converted to a character form without using exponential notation. In this case, if the scale is zero then no decimal point is added and if the scale is positive a decimal point will be inserted with the scale specifying the number of characters to the right of the decimal point.

如果您想要没有 e10 符号的输出,请使用 toPlainString() .

关于java - 为什么输出是科学计数法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30352694/

相关文章:

php - 使用 PHP 将空格转换为破折号和小写

sql - Oracle中DataReader读取两个长小数相除结果时抛出 'Specified cast not valid'

java - gwt - 在 RPC 调用中使用 List<Serializable>?

java - 需要有关 java 中复杂结构的建议(DAO 和服务层链接/耦合)

java - 如何判断app是否被 "Share-Via"调用?

java - 能否提供有关如何使用 Java 中的 FMJ 或 JMF 流式传输音频文件的示例

c++ - 命名空间中的 "string is not a type"

Python 字符串先进先出

java - 如何在java中的while循环中将数字放入数组?

python - 从 Ignition 中的串行读取十六进制或十进制