加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 为什么输出是科学记数法?

发布时间:2020-12-15 04:27:44 所属栏目:Java 来源:网络整理
导读:为什么以下代码的输出是科学记数法? BigDecimal val = new BigDecimal("0000.000000111"); System.out.println(val); 输出:1.11e-8 但此代码的输出以十进制格式正确. BigDecimal val = new BigDecimal("0000.000022222"); System.out.println(val); 输出:
为什么以下代码的输出是科学记数法?

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

输出:1.11e-8

但此代码的输出以十进制格式正确.

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

输出:0.000022222

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

解决方法

如果你看一下 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().

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读