BigDecimal的基本运算及转换
BigDecimal
Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算。双精度浮点型变量double可以处理16位有效数。在实际应用中,需要对更大或者更小的数进行运算和处理。float和double只能用来做科学计算或者是工程计算,在商业计算中要用java.math.BigDecimal。BigDecimal所创建的是对象,我们不能使用传统的+、-、*、/等算术运算符直接对其对象进行数学运算,而必须调用其相对应的方法。方法中的参数也必须是BigDecimal的对象。构造器是类的特殊方法,专门用来创建对象,特别是带有参数的对象。
1 基本运算
1. 加
add()
BigDecimal bigDecimal1 = BigDecimal.valueOf(5.2);
BigDecimal bigDecimal2 = BigDecimal.valueOf(2.2D);
BigDecimal add = bigDecimal1.add(bigDecimal2);
System.out.println("bigDecimal = " + add);
2. 减
subtract()
BigDecimal bigDecimal1 = BigDecimal.valueOf(5.2);
BigDecimal bigDecimal2 = BigDecimal.valueOf(2.2D);
BigDecimal add = bigDecimal1.subtract(bigDecimal2);
System.out.println("bigDecimal = " + add);
3. 乘
multiply()
BigDecimal bigDecimal1 = BigDecimal.valueOf(5);
BigDecimal bigDecimal2 = BigDecimal.valueOf(2);
BigDecimal add = bigDecimal1.multiply(bigDecimal2);
System.out.println("bigDecimal = " + add);
4. 除
BigDecimal bigDecimal1 = BigDecimal.valueOf(5);
BigDecimal bigDecimal2 = BigDecimal.valueOf(2);
BigDecimal add = bigDecimal1.divide(bigDecimal2);
System.out.println("bigDecimal = " + add);
2 数据类型转换
valueOf():可传double,long,int转换为BigDecimal类型
1. int
BigDecimal bigDecimal = BigDecimal.valueOf(2);
int i = bigDecimal.intValue();
2. double
BigDecimal bigDecimal = BigDecimal.valueOf(2.2);
double v = bigDecimal.doubleValue();
3. String
BigDecimal bigDecimal =new BigDecimal("2.2");
String string = bigDecimal.toString();
使用String可解决精度丢失问题
4. long
BigDecimal bigDecimal = BigDecimal.valueOf(2L);
long l = bigDecimal.longValue();
3 小数点的处理
setScale(保留小数点的位数,保留小数点的策略方案)
BigDecimal.ROUND_UP:只要第n位小数不为0都进1
BigDecimal test= new BigDecimal("9.56");
// 1表示保留一位小数,BigDecimal.ROUND_UP:只要第二位小数不为0都进1
System.out.println(test.setScale(1,BigDecimal.ROUND_UP));
BigDecimal test1= new BigDecimal("9.0");
// 1表示保留一位小数,BigDecimal.ROUND_UP:只要第二位小数不为0都进1
System.out.println(test.setScale(1,BigDecimal.ROUND_UP));
BigDecimal.ROUND_DOWN:直接删除多余的小数位
BigDecimal test= new BigDecimal("9.56666");
//直接删除多余的小数位
System.out.println(test.setScale(1,BigDecimal.ROUND_DOWN));
BigDecimal.ROUND_HALF_UP:四舍五入
BigDecimal test= new BigDecimal("9.56666");
//直接删除多余的小数位
System.out.println(test.setScale(1,BigDecimal.ROUND_HALF_UP));
BigDecimal test1= new BigDecimal("9.54444");
//直接删除多余的小数位
System.out.println(test1.setScale(1,BigDecimal.ROUND_HALF_UP));
4 BigDecimal的比较
BigDecimal比较大小用compareTo()方法,大于为1,小于为-1,等于为0
public int compareTo( NumberSubClass referenceName )
参数:
referenceName – 可以是一个 Byte, Double, Integer, Float, Long, BigDecimal或 Short 类型的参数。
返回值:
-
如果指定的数与参数相等返回
0
。 -
如果指定的数小于参数返回
-1
。 -
如果指定的数大于参数返回
1
。
BigDecimal b1 = BigDecimal.valueOf(100);
BigDecimal b2 = BigDecimal.valueOf(0);
BigDecimal b3 = BigDecimal.valueOf(0);
int i = b1.compareTo(b2);
System.out.println("i = " + i);// 1
System.out.println(i > 0);// true
int i1 = b1.compareTo(b2);
System.out.println(i1 == 1);//返回true
int i2 = b1.compareTo(b2);
System.out.println(i2 == -1);//返回false
int i3 = b3.compareTo(b2);
System.out.println(i3 == 0);//返回true
5. BigDecimal的函数
1. signum()
返回BigDecimal的值为负、零或正时,取值为-1、0或1。
示例:
if (occurBalance.signum() != -1) {
return Wrapper.fail(ResponseCode.REQUEST_PARAMETER_EXCEPTION, "金额应为负数");
}
2. abs()
返回BigDecimal的绝对值
示例:
BigDecimal afterFrozenBalance = frozenBalance.add(occurBalance.abs());
6. 练习
1. 练习1
v为装有BigDecimal的集合
需求:只要大于0的BigDecimal
// compareTo()b1大于b2返回1,条件设置为>0,排除了compareTo()返回0或-1的情况,也就是排除了两个参数相等和小于的情况
List<BigDecimal> price = v.stream().filter(a -> a.getRetailPrice().compareTo(BigDecimal.ZERO) > 0).collect(Collectors.toList());
2. 练习2
使用stream累加BigDecimal
public class BigDecimalSumExample {
static class WaybillOpenOuterOrderGoodsReqVO {
private BigDecimal shippedQuantity;
public BigDecimal getShippedQuantity() {
return shippedQuantity;
}
public void setShippedQuantity(BigDecimal shippedQuantity) {
this.shippedQuantity = shippedQuantity;
}
}
public static void main(String[] args) {
// 创建一个列表并初始化
List<WaybillOpenOuterOrderGoodsReqVO> orderGoodsList = new ArrayList<>();
orderGoodsList.add(new WaybillOpenOuterOrderGoodsReqVO());
orderGoodsList.get(0).setShippedQuantity(new BigDecimal("100.5"));
orderGoodsList.add(new WaybillOpenOuterOrderGoodsReqVO());
orderGoodsList.get(1).setShippedQuantity(new BigDecimal("200.75"));
orderGoodsList.add(new WaybillOpenOuterOrderGoodsReqVO());
orderGoodsList.get(2).setShippedQuantity(new BigDecimal("300.0"));
// 使用stream累加shippedQuantity属性
BigDecimal totalShippedQuantity = orderGoodsList.stream()
.map(WaybillOpenOuterOrderGoodsReqVO::getShippedQuantity) // 将流中的每个对象映射为其shippedQuantity
.reduce(BigDecimal.ZERO, BigDecimal::add); // 使用reduce进行累加,初始值为BigDecimal.ZERO
System.out.println("Total Shipped Quantity: " + totalShippedQuantity);
}
}
// 创建BigDecimal数组
BigDecimal[] bigDecimals = { new BigDecimal("10.5"), new BigDecimal("20.75"), new BigDecimal("30.0") };
// 使用stream进行累加
BigDecimal sum = Arrays.stream(bigDecimals)
.reduce(BigDecimal.ZERO, BigDecimal::add);
System.out.println("Sum of BigDecimal numbers: " + sum);