当前位置:网站首页>BigDecimal's addition, subtraction, multiplication and division, size comparison, rounding up and down, and BigDecimal's set accumulation, judge whether BigDecimal has decimal
BigDecimal's addition, subtraction, multiplication and division, size comparison, rounding up and down, and BigDecimal's set accumulation, judge whether BigDecimal has decimal
2022-07-26 10:47:00 【Eric-x】
In fact, online about Bigdecimal There are many documents about the operation of , Here is a summary , Put these routine operations into a document , For future viewing
Bigdecimal Addition, subtraction, multiplication and division
BigDecimal b1 = new BigDecimal("10");
BigDecimal b2 = new BigDecimal("5");
BigDecimal b3 = null;
// Add
b3 = b1.add(b2);
System.out.println("b1 + b2 =" + b3);// Output 15
// Subtraction
b3 = b1.subtract(b2);
System.out.println("b1 - b2 = " + b3);// Output 5
// Multiplication
b3 = b1.multiply(b2);
System.out.println("b1 * b2 = " + b3);// Output 50
// division
b3 = b1.divide(b2);
System.out.println("b1 / b2 = " + b3);// Output 2
Bigdecimal Compare the size of
BigDecimal a = new BigDecimal("10");
BigDecimal b = new BigDecimal("5");
if(a.compareTo(b) == -1){
System.out.println("a Less than b");
}
if(a.compareTo(b) == 0){
System.out.println("a be equal to b");
}
if(a.compareTo(b) == 1){
System.out.println("a Greater than b");
}
if(a.compareTo(b) > -1){
System.out.println("a Greater than or equal to b");
}
if(a.compareTo(b) < 1){
System.out.println("a Less than or equal to b");
}
Bigdecimal Rounding up and rounding down
BigDecimal a = new BigDecimal("0.9");
System.out.println(" Rounding down :" + a.setScale(0,BigDecimal.ROUND_FLOOR));
BigDecimal b = new BigDecimal("0.1");
System.out.println(" Rounding up :" + b.setScale(0,BigDecimal.ROUND_UP));
Judge BigDecimal Is there a decimal
BigDecimal b = new BigDecimal(20.5);
if (new BigDecimal(b.intValue()).compareTo(b) != 0) {
System.out.println("b Is the decimal ");
} else {
System.out.println("b Is an integer ");
}
Bigdecimal Set accumulation of
image Bigdecimal Cumulative scenes are rare , Although not common , But there will be , unfortunately , I'll use it today ( Smile to cry ~), So also record this .
There are two scenarios , One is that the set is directly Bigdecimal Of , as follows
List<BigDecimal> list = new ArrayList<>();
Another is in the form of set objects , Then there are Bigdecimal Properties of type , as follows
List<Employee> list = new ArrayList<>();
// You can see Employee There is an attribute in the class salary, The type is Bigdecimal
@Data
public class Employee {
private int id;
private String name;
private int age;
private BigDecimal salary;
}
Accumulation method of scenario one
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); // Output 6
Accumulation method of scenario 2 : Get... From the object
List<Employee> list = new ArrayList<>();
list.add(new Employee(1," Wang ba ",BigDecimal.valueOf(5)));
list.add(new Employee(2," Chu Yuan ",BigDecimal.valueOf(10)));
BigDecimal result = list.stream()
// take Employee Object's salary out map Return to Bigdecimal
.map(Employee::getSalary)
// Use reduce() Aggregate functions , Implement accumulator
.reduce(BigDecimal.ZERO,BigDecimal::add);
System.out.println(result);// Output 15
The above two methods mainly use JDK8 In new features Stream, If you don't understand, you can check this article :JDK8 New characteristics - Detailed explanation
because Bigdecimal Similar ones are commonly used in actual development , Make a special note to deepen your influence , Of course , If it helps you , It's also my pleasure ~
边栏推荐
- LIst和Dictionary实例应用(※)
- 2021-08-14 Sanzi chess
- IAR sprintf 浮点 在UCOS 总格式化成0.0的问题
- 微信公众号开发 获取openid时报错40029 invalid code 问题的解决
- Flutter TextField怎样去除下划线及有焦点时颜色
- [leetcode daily question 2021/8/31] 1109. Flight reservation statistics [medium] differential array
- router.push(),router.repalce(),router.go()使用
- 2021-08-13 learn C language with pengge - array
- 一文详解Nodejs中fs文件模块与path路径模块
- [leetcode daily question 2021/2/18] [detailed explanation] minimum number of turns of 995. K continuous bits
猜你喜欢

0x00007FFD977C04A8 (Qt5Sqld.dll)处(位于 a.exe 中)引发的异常: 0xC0000005: 读取位置 0x0000000000000010 时发生访问冲突

kali 查看ip地址

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

232.用栈实现队列

349. 两个数组的交集

RT thread learning notes (VII) -- open the elmfat file system based on SPI flash (middle)

Add touch screen driver for stemwin 5.22 on Shenzhou IV development board

2021-08-12函数递归_和鹏哥学习C语言

Issue 5: the second essential skill for College Students

IAR sprintf 浮点 在UCOS 总格式化成0.0的问题
随机推荐
Sql Server 数据库之完整性约束
mysql20210906
剑指Offer(四十四):翻转单词顺序序列
[machine learning notes] [face recognition] deeplearning ai course4 4th week programming
Issue 5: the second essential skill for College Students
Happens-Before原则深入解读
MySQL速学笔记-2021-08-31
使用grid实现左中右布局,中间内容自适应
px2rem-loader将px转化为rem,适配移动端vant-UI等框架
2021-08-12函数递归_和鹏哥学习C语言
232.用栈实现队列
$router和$route的区别
解决:无法加载文件 C:\Users\user\AppData\Roaming\npm\npx.ps1,因为在此系统上禁止运行脚本 。
WinPcap packet capturing function pcap_ Loop (), stop the problem
11 handle "self assignment" in operator=
Pengge C language - minesweeping 2021-08-16
[leetcode daily question 2021/5/8]1723. The shortest time to complete all work
putty的使用教程
为什么需要自动化测试?软件测试师带你测评不同软件测试工具
微信公众号开发 获取openid时报错40029 invalid code 问题的解决