当前位置:网站首页>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 ~
边栏推荐
猜你喜欢

在altium designer中禁用USBJATG

RT thread learning notes (VI) -- start the elmfat file system based on SPI flash (Part 1)

IAR sprintf 浮点 在UCOS 总格式化成0.0的问题

Phase 4: one of College Students' vocational skills preparation in advance

RT-Thread 学习笔记(三)---用SCons 构建编译环境

反射机制简述

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

RT thread learning notes (V) -- edit, download and debug programs

Zongzi battle - guess who can win

344.反转字符串
随机推荐
Flutter TextField怎样去除下划线及有焦点时颜色
Sword finger offer (twenty): stack containing min function
Pengge C language sixth class
12 don't forget every component when copying an object
字典与int矩阵
The problem of formatting IAR sprintf floating point to 0.0 in UCOS assembly
2021-08-14 Sanzi chess
访问权限——private,public,protected
Error[pe147]: declaration is incompatible with 'error problem
JS对象赋值问题
剑指Offer(四十四):翻转单词顺序序列
鹏哥C语言第六节课
Common classes (understand)
Sql Server 之SQL语句对基本表及其中的数据的创建和修改
SQL Server 之Sql语句创建数据库
微信公众号开发 获取openid时报错40029 invalid code 问题的解决
剑指Offer(二十):包含min函数的栈
2021-08-13 learn C language with pengge - array
Sql Server 数据库之完整性约束
回到顶部的几种方案(js)