举例
public static void main(String[] args) {
System.out.println(new BigDecimal("0.1"));//0.1
System.out.println(new BigDecimal(0.1));//0.1000000000000000055511151231257827021181583404541015625
}
BigDecimal构造器
/* The results of this constructor can be somewhat unpredictable.
* One might assume that writing {@code new BigDecimal(0.1)} in
* Java creates a {@code BigDecimal} which is exactly equal to
* 0.1 (an unscaled value of 1, with a scale of 1), but it is
* actually equal to
* 0.1000000000000000055511151231257827021181583404541015625.
* This is because 0.1 cannot be represented exactly as a
* {@code double} (or, for that matter, as a binary fraction of
* any finite length). Thus, the value that is being passed
* <i>in</i> to the constructor is not exactly equal to 0.1,
* appearances notwithstanding.
* /
public BigDecimal(double val) {
this(val,MathContext.UNLIMITED);
}
double小数,装换成BigDecimal,直接用构造器,不准确。要+空串,变成String类型后,使用构造器
如String a = val + “”;
public BigDecimal(String val) {
this(val.toCharArray(), 0, val.length());
}