澄清BigDecimal.stripTrailingZeroes()的行为 [英] Clarification on behavior of BigDecimal.stripTrailingZeroes()

查看:131
本文介绍了澄清BigDecimal.stripTrailingZeroes()的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么下面的代码显示的是0.00而不是0?

Why the following code prints 0.00 and not 0?

BigDecimal big = new BigDecimal("0.00");
big = big.stripTrailingZeros();
System.out.println(big.toPlainString());

以下是stripTrailingZeroes的文档:

The following is the documentation for stripTrailingZeroes:

返回BigDecimal,该数字在数值上等于此1,但从表示形式中删除了所有尾随零.例如,从BigDecimal值600.0(其[BigInteger,scale]分量等于[6000,1])中去除尾随零,将产生6E2,其中[BigInteger,scale]分量等于[6,-2]

返回:
一个数值相等的BigDecimal,其中删除了所有尾随零.
Returns BigDecimal which is numerically equal to this one but with any trailing zeros removed from the representation. For example, stripping the trailing zeros from the BigDecimal value 600.0, which has [BigInteger, scale] components equals to [6000, 1], yields 6E2 with [BigInteger, scale] components equals to [6, -2]

Returns:
a numerically equal BigDecimal with any trailing zeros removed.

推荐答案

它将去除内部表示形式[6000,1]的尾随零,并相应地调整小数位数1 -> -2.内部表示形式在某种程度上也与最终的打印输出无关,最终的打印输出还受格式,舍入和区域设置的影响.

It strips the trailing zeroes of the internal representation [6000,1] and adapts the scale accordingly 1 -> -2. The internal representation is also somewhat independent of the final print-out which is also affected by format, rounding and locale.

这篇关于澄清BigDecimal.stripTrailingZeroes()的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆