大小数。 multiply() 和 divide() 方法 return 十六进制数。为什么?
BigDecimal. multiply() and divide() methods return hexadecimal number. Why?
这是我的代码:
public class Test1 {
public static void main(String[] args) {
BigDecimal wallet = new BigDecimal("0.0");
BigDecimal productPrice = new BigDecimal("0.01");
for (int i = 1; i <= 5; i++) {
wallet = wallet.multiply(productPrice);
}
System.out.println(wallet);
}
}
结果:0E-11
我有个问题。为什么我得到的结果是十六进制而不是十进制?像这样:2.45
这不是十六进制,它是 the scientific notation as evaluated by the toString()
方法:
Returns the string representation of this BigDecimal
, using scientific notation if an exponent is needed.
E
字母表示数字的指数。
一般情况下,如果要格式化十进制数,可以使用java.text.DecimalFormat
。
在您的情况下,使用的方法 toString
将在需要时使用指数字段。
如果您不想要带有指数字段的字符串表示,您可以使用 toPlainString
。
System.out.println(wallet.toPlainString());
这是我的代码:
public class Test1 {
public static void main(String[] args) {
BigDecimal wallet = new BigDecimal("0.0");
BigDecimal productPrice = new BigDecimal("0.01");
for (int i = 1; i <= 5; i++) {
wallet = wallet.multiply(productPrice);
}
System.out.println(wallet);
}
}
结果:0E-11
我有个问题。为什么我得到的结果是十六进制而不是十进制?像这样:2.45
这不是十六进制,它是 the scientific notation as evaluated by the toString()
方法:
Returns the string representation of this
BigDecimal
, using scientific notation if an exponent is needed.
E
字母表示数字的指数。
一般情况下,如果要格式化十进制数,可以使用java.text.DecimalFormat
。
在您的情况下,使用的方法 toString
将在需要时使用指数字段。
如果您不想要带有指数字段的字符串表示,您可以使用 toPlainString
。
System.out.println(wallet.toPlainString());