当前位置:网站首页>Bigdecimal的加减乘除、比较大小、向上向下取整 和 Bigdecimal的集合累加、判断BigDecimal是否有小数
Bigdecimal的加减乘除、比较大小、向上向下取整 和 Bigdecimal的集合累加、判断BigDecimal是否有小数
2022-07-26 10:46:00 【Eric-x】
其实在网上关于Bigdecimal的操作有很多文献,这里就当做个总结,将这些常规操作放到一篇文献,方便日后查看
Bigdecimal的加减乘除
BigDecimal b1 = new BigDecimal("10");
BigDecimal b2 = new BigDecimal("5");
BigDecimal b3 = null;
//加法
b3 = b1.add(b2);
System.out.println("b1 + b2 =" + b3);//输出15
//减法
b3 = b1.subtract(b2);
System.out.println("b1 - b2 = " + b3);//输出5
//乘法
b3 = b1.multiply(b2);
System.out.println("b1 * b2 = " + b3);//输出50
//除法
b3 = b1.divide(b2);
System.out.println("b1 / b2 = " + b3);//输出2
Bigdecimal的比较大小
BigDecimal a = new BigDecimal("10");
BigDecimal b = new BigDecimal("5");
if(a.compareTo(b) == -1){
System.out.println("a小于b");
}
if(a.compareTo(b) == 0){
System.out.println("a等于b");
}
if(a.compareTo(b) == 1){
System.out.println("a大于b");
}
if(a.compareTo(b) > -1){
System.out.println("a大于等于b");
}
if(a.compareTo(b) < 1){
System.out.println("a小于等于b");
}
Bigdecimal的向上取整和向下取整
BigDecimal a = new BigDecimal("0.9");
System.out.println("向下取整:" + a.setScale(0,BigDecimal.ROUND_FLOOR));
BigDecimal b = new BigDecimal("0.1");
System.out.println("向上取整:" + b.setScale(0,BigDecimal.ROUND_UP));
判断BigDecimal是否有小数
BigDecimal b = new BigDecimal(20.5);
if (new BigDecimal(b.intValue()).compareTo(b) != 0) {
System.out.println("b 是小数");
} else {
System.out.println("b 是整数");
}
Bigdecimal的集合累加
像Bigdecimal累加的场景比较少见,虽然不常见,但还是会有,不巧,我今天就用到了(笑哭~),所以也记录一下这个。
有两种场景,一种是集合直接为Bigdecimal的,如下
List<BigDecimal> list = new ArrayList<>();
还一种就是集合对象形式的,然后该对象中有Bigdecimal类型的属性,如下
List<Employee> list = new ArrayList<>();
//可以看到Employee类中有一个属性salary,类型为Bigdecimal
@Data
public class Employee {
private int id;
private String name;
private int age;
private BigDecimal salary;
}
场景一的累加方式
List<BigDecimal> list = new ArrayList<>();
list.add(new BigDecimal("1"));
list.add(new BigDecimal("2"));
list.add(new BigDecimal("3"));
BigDecimal decimal = list.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
System.out.println(decimal); //输出6
场景二的累加方式:从对象中获取
List<Employee> list = new ArrayList<>();
list.add(new Employee(1,"王霸",BigDecimal.valueOf(5)));
list.add(new Employee(2,"楚元",BigDecimal.valueOf(10)));
BigDecimal result = list.stream()
// 将Employee对象的salary取出来 map返回为Bigdecimal
.map(Employee::getSalary)
// 使用reduce()聚合函数,实现累加器
.reduce(BigDecimal.ZERO,BigDecimal::add);
System.out.println(result);//输出15
上面这两种方式用的主要是JDK8新特性中的Stream,不理解的小伙伴可以查看这篇文章:JDK8新特性-详解
因为Bigdecimal类似在实际开发中还是比较常用的,特意记录一下加深下自己的影响,当然,如果对你也有帮助,那也是我的荣幸~
边栏推荐
- 剑指Offer(四十四):翻转单词顺序序列
- Pengge C language 20210811 program structure operation
- 用两个栈实现队列
- Disable usbjatg in Altium Designer
- Add touch screen driver for stemwin 5.22 on Shenzhou IV development board
- 剑指Offer(五十三):表示数值的字符串
- Summary of the seventh class of pengge C language
- 剑指Offer(九):变态跳台阶
- 剑指Offer(二十一):栈的压入、弹出序列
- Flutter jni混淆 引入.so文件release包闪退
猜你喜欢

349. 两个数组的交集

2021-08-12函数递归_和鹏哥学习C语言
控制随机抽中几率 [ C# | Random ]

RT thread learning notes (III) -- building a compilation environment with scons

sigmod 函数与softmax 函数对比
![[paper after dinner] deep mining external perfect data for chestx ray disease screening](/img/d6/41c75d292c26b2e7e116767a51eb5e.png)
[paper after dinner] deep mining external perfect data for chestx ray disease screening

抽象工厂及其改进示例

工厂模式详解

文案秘籍七步曲至----文献团队协作管理

0x00007FFD977C04A8 (Qt5Sqld.dll)处(位于 a.exe 中)引发的异常: 0xC0000005: 读取位置 0x0000000000000010 时发生访问冲突
随机推荐
扫雷pro版2021-08-19
Flutter CachedNetworkImage圆角
RT-Thread 学习笔记(六)--- 开启基于SPI Flash的elmfat文件系统(上)
RT thread learning notes (I) -- configure RT thread development environment
The problem of formatting IAR sprintf floating point to 0.0 in UCOS assembly
鹏哥C语言20210811程序结构作业
RT thread learning notes (VIII) -- start the elmfat file system based on SPI flash (Part 2)
SAP ABAP 守护进程的实现方式
JS对象赋值问题
剑指Offer(八):跳台阶
Sql Server之查询总结
RT-Thread 学习笔记(八)---开启基于SPI Flash的elmfat文件系统(下)
Summary of the seventh class of pengge C language
用两个栈实现队列
PLC与伺服电机连接
Happens-Before原则深入解读
flutter dart生成N个区间范围内不重复随机数List
抽象工厂及其改进示例
242.有效的字母异位词
Analysis of the transaction problem of chained method call