构造方法
最佳实践
一般使用
BigDecimal(String val)
//创建对应数值的对象
BigDecimal(String val)
System.out.println(new BigDecimal("123"));
System.out.println(new BigDecimal("123.1"));
System.out.println(new BigDecimal("123.23"));
System.out.println(new BigDecimal("123.345"));
System.out.println(new BigDecimal("0"));
System.out.println(new BigDecimal("-123"));
System.out.println(new BigDecimal("-123.1"));
System.out.println(new BigDecimal("-123.23"));
123
123.1
123.23
123.345
0
-123
-123.1
-123.23
//创建并对数值进行舍入 及 有效位数处理 (注意舍入处理不会补零)
BigDecimal(String val, MathContext mc)
System.out.println(new BigDecimal("123.4", new MathContext(4,
RoundingMode.HALF_UP)));
System.out.println(new BigDecimal("123.4", new MathContext(2,
RoundingMode.HALF_UP)));
System.out.println(new BigDecimal("125.4", new MathContext(2,
RoundingMode.CEILING)));
System.out.println(new BigDecimal("123.4", new MathContext(1,
RoundingMode.CEILING)));
13.45
1.3E+2
1.3E+2
2E+2
BigDecimal(double val) //会出现精度问题
System.out.println(new BigDecimal(0.1));
System.out.println(new BigDecimal(0.2));
System.out.println(new BigDecimal(0.3));
System.out.println(new BigDecimal(0.4));
System.out.println(new BigDecimal(0.5));
0.1000000000000000055511151231257827021181583404541015625
0.200000000000000011102230246251565404236316680908203125
0.299999999999999988897769753748434595763683319091796875
0.40000000000000002220446049250313080847263336181640625
0.5
BigDecimal(double val,MathContext mc)
System.out.println(new BigDecimal(0.5,new MathContext(20, RoundingMode.HALF_UP)));
System.out.println(new BigDecimal(0.4,new MathContext(20, RoundingMode.HALF_UP)));
System.out.println(new BigDecimal(0.3,new MathContext(20, RoundingMode.HALF_UP)));
System.out.println(new BigDecimal(0.2,new MathContext(20, RoundingMode.HALF_UP)));
System.out.println(new BigDecimal(0.1,new MathContext(20, RoundingMode.HALF_UP)));
0.5
0.40000000000000002220
0.29999999999999998890
0.20000000000000001110
0.10000000000000000555
同理整数类型就不演示了
BigDecimal(int val)
BigDecimal(long val)
BigDecimal(int val,MathContext mc)
BigDecimal(long val,MathContext mc)
float 和 double 转 bigdecimal
System.out.println(new BigDecimal(Float.toString(0.1f)));
System.out.println(new BigDecimal(Double.toString(0.1)));
0.1
0.1
加减乘除方法
BigDecimal a = new BigDecimal(Float.toString(0.1f));
BigDecimal b = new BigDecimal(Double.toString(1.23));
System.out.println(a.add(b));
//将结果保留两位有效数字,使用4舍5入
System.out.println(a.add(b,new MathContext(2, RoundingMode.HALF_UP)));
1.33
1.3
System.out.println(a.subtract(b));
System.out.println(a.subtract(b,new MathContext(2, RoundingMode.HALF_UP)));
-0.23
-0.23
System.out.println(a.multiply(b));
System.out.println(a.multiply(b,new MathContext(2, RoundingMode.HALF_UP)));
1.230
1.2
//该方法如果除不净 报 ArithmeticException
//System.out.println(a.divide(b));//基本不用
System.out.println(a.divide(b,new MathContext(2,RoundingMode.HALF_UP)));
//保留小数位数 为this.scale()
System.out.println(a.divide(b,RoundingMode.HALF_UP));
//指定保留小数位数
System.out.println(a.divide(b,4,RoundingMode.HALF_UP));
//这里的舍入方式也可以使用BigDecimal.ROUND_HALF_UP等的方式
System.out.println(a.divide(b,4,BigDecimal.ROUND_HALF_UP));
0.81
0.8
0.8130
0.8130
//获取除数结果的整数部分
System.out.println(a.divideToIntegralValue(b,new MathContext(2,RoundingMode.HALF_UP)));
//将结果不以科学计数法显示
System.out.println(a.divideToIntegralValue(b,new MathContext(2,RoundingMode.HALF_UP)).toPlainString());
0E+2
0
//获取结果的小数部分
比较相等
1.转成int
BigDecimal b1 = new BigDecimal("-121454125453.145");
if(b1.intValue()<0) {
System.out.println("金额为负数!");
System.out.println(b1);
}
2.compareTo方法
结果是: -1:小于; 0 :等于; 1 :大于;
BigDecimal b1 = new BigDecimal("-121454125453.145");
if(b1.compareTo(BigDecimal.ZERO)==-1) {
System.out.println("金额为负数!");
}