当前位置:网站首页>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类似在实际开发中还是比较常用的,特意记录一下加深下自己的影响,当然,如果对你也有帮助,那也是我的荣幸~
边栏推荐
- RT-Thread 学习笔记(五)---编辑、下载、调试程序
- MySQL quick learning notes-2021-08-31
- 13 managing resources by objects
- Simple use of json-c Library -- converting JSON files to struct
- Constructors, method overloads, object arrays, and static
- Build ARM embedded development environment
- 剑指Offer(二十):包含min函数的栈
- Common classes (understand)
- 剑指Offer(四十三):左旋转字符串
- 使用float实现左中右布局,中间内容自适应
猜你喜欢

20210807 1 c language program structure

Zongzi battle - guess who can win

35. 搜索插入位置

GIS方法类期刊和论文的综述(Introduction)怎么写?

在神州IV开发板上为STemWin 5.22加入触屏驱动
![[leetcode daily question 2021/2/13]448. Find all the missing numbers in the array](/img/9b/624416fa6a408bf64ca5438273176b.png)
[leetcode daily question 2021/2/13]448. Find all the missing numbers in the array

Add touch screen driver for stemwin 5.22 on Shenzhou IV development board
![[notes on machine learning] [building a cyclic neural network and its application] deeplearning ai course5 1st week programming(keras)](/img/02/f85da2a2f2524fb034b17ed8d06692.png)
[notes on machine learning] [building a cyclic neural network and its application] deeplearning ai course5 1st week programming(keras)

kali 查看ip地址
![[machine learning notes] [style transfer] deeplearning ai course4 4th week programming(tensorflow2)](/img/94/ff52b043320b6dea5ca1238e314de8.png)
[machine learning notes] [style transfer] deeplearning ai course4 4th week programming(tensorflow2)
随机推荐
Flutter报错 Incorrect use of ParentDataWidget When the exception was thrown, this was the stack:
0x00007FFD977C04A8 (Qt5Sqld.dll)处(位于 a.exe 中)引发的异常: 0xC0000005: 读取位置 0x0000000000000010 时发生访问冲突
PLC与伺服电机连接
剑指Offer(二十一):栈的压入、弹出序列
Pengge C language sixth class
Summary of the seventh class of pengge C language
putty的使用教程
Sql Server 数据库之完整性约束
RT-Thread 学习笔记(三)---用SCons 构建编译环境
[leetcode daily question 2021/2/13]448. Find all the missing numbers in the array
英语基础句型结构------起源
Codepoint 58880 not found in font, aborting. flutter build apk时报错
智能合约dapp系统开发流程技术
shell 脚本 失败自动重复执行
JS对象赋值问题
flutter dart生成N个区间范围内不重复随机数List
router.push(),router.repalce(),router.go()使用
Pengge C language 20210811 program structure operation
面试知识点
MFC中0x003b66c3 处有未经处理的异常: 0xC000041D: 用户回调期间遇到未经处理的异常