BigDecimal investment = new BigDecimal("0.00"); investment = investment.add( new BigDecimal("100.00")); System.out.print("investment:" + investment);//100.00
2. 减法
1 2 3 4
public BigDecimal subtract(BigDecimal subtrahend) public BigDecimal subtract(BigDecimal subtrahend, MathContext mc)
MathContext : 设置保留几位小数,舍入按四舍五入
3. 乘法
1 2 3 4
public BigDecimal multiply(BigDecimal multiplicand) public BigDecimal multiply(BigDecimal multiplicand, MathContext mc)
MathContext : 设置保留几位小数,舍入按四舍五入
4. 除法
1 2 3 4 5 6
public BigDecimal divide(BigDecimal divisor) public BigDecimal divide(BigDecimal divisor, MathContext mc) public BigDecimal divide(BigDecimal divisor, int roundingMode) public BigDecimal divide(BigDecimal divisor, RoundingMode roundingMode) public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode) public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)
Inspection info: Reports calls to divide() or setScale() without a rounding mode argument. Such calls can lead to an ArithmeticException when the exact value cannot be represented in the result (e.g. because it has a non-terminating decimal expansion). Specifying a rounding mode prevents the ArithmeticException.
public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode) public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)