double类型转BigDecimal的正确姿势有两种
- BigDecimal.valueOf(Double_Value)
- new BigDecimal(String.valueOf(Double_Value))
原因:
BigDecimal最佳实践是支持String类型的构造方法。
Note: For values other than float and double NaN and ±Infinity, this constructor is compatible with the values returned by Float.toString and Double.toString. This is generally the preferred way to convert a float or double into a BigDecimal, as it doesn’t suffer from the unpredictability of the BigDecimal(double) constructor.
public BigDecimal(String val) {
this(val.toCharArray(), 0, val.length());
}