import java.math.BigDecimal;
public class NP_BigDecimal {
public static void main(String[] args) {
/**
* values are String's type.
*/
BigDecimal a = new BigDecimal("1");
BigDecimal b = new BigDecimal("8");
/**
* show "+ - * /"
*/
BigDecimal result;
result = a.add(b);
result = a.subtract(b);
result = a.multiply(b);
result = a.divide(b, 2, BigDecimal.ROUND_HALF_DOWN);
System.out.println(result);
/**
* 四舍五入
* 2 为保留多少位小数,BigDecimal.ROUND_HALF_DOWN 为 四舍五入的参数
*/
result = result.setScale(2, BigDecimal.ROUND_HALF_DOWN);
System.out.println(result);
}
}
来源:CSDN
作者:Yuanxuan0210
链接:https://blog.csdn.net/m0_46346380/article/details/104611167