当前位置:网站首页>BigDecimal common API
BigDecimal common API
2022-07-28 01:17:00 【Big dream_ Thousands of autumn】
BigDecimal Commonly used API
BigDecimal Commonly used in daily development , Some calculations involving amounts and calculations to ensure accuracy will use , Here are some common API
1、 Add add
BigDecimal a = new BigDecimal(999);
BigDecimal b = new BigDecimal(10000);
a = a.add(b);
System.out.println(a.add(b));//10999
2、 Subtraction subtract
BigDecimal a = new BigDecimal(999);
BigDecimal b = new BigDecimal(10000);
a = a.subtract(b);
System.out.println(a);//-9001
3、 Multiplication multiply
BigDecimal a = new BigDecimal(999);
BigDecimal b = new BigDecimal(10000);
BigDecimal c = a.multiply(b);
System.out.println("a*b=" + c);
4、 division divide
BigDecimal a = new BigDecimal(999);
BigDecimal b = new BigDecimal(10000);
c = a.divide(b);
System.out.println("a/b=" + c);
5、 Seeking remainder divideAndRemainder
divideAndRemainder Method will return a length of 2 Of BigDecimal Array , The first is business , The second is the remainder
BigDecimal a = new BigDecimal(999);
BigDecimal b = new BigDecimal(10000);
BigDecimal[] c = a.divideAndRemainder(b);
System.out.println("a/b=" + c[0] + " remainder " + c[1]);
6、 Compare the size compareTo
BigDecimal Can pass compareTo Methods to compare .
The result is int type ,-1 Say less than ,0 Is equal to ,1 It is greater than .
BigDecimal a = new BigDecimal(999);
BigDecimal b = new BigDecimal(10000);
if (a.compareTo(b) > 0) {
System.out.println("a Greater than b");
} else if (a.compareTo(b) == 0) {
System.out.println("a be equal to b");
} else {
System.out.println("a Less than b");
}
7、 Deal with decimals
Here is the trade-off mode
- ROUND_UP : For positive numbers, it is to discard the decimal , Add one , For negative numbers , Subtract one from the decimal . for example :100.4, Take the integer as 101,-100.5, Take the integer as -101
- ROUND_DOWN : Directly discard decimals or decimals after the exact place
- ROUND_CEILING : If BigDecimal It's true , Then do ROUND_UP operation ; If a negative , Then do ROUND_DOWN operation ( Take the larger integer in the vicinity ).
- ROUND_FLOOR : If BigDecimal It's true , Then do ROUND_DOWN operation ; If a negative , Then do ROUND_UP operation ( Take the smaller integer in the vicinity ).
- ROUND_HALF_UP : rounding ,0.5 Rounding up 100.5 -> 101
- ROUND_HALF_DOWN : rounding ,0.5 Rounding down 100.5 -> 100
- ROUND_UNNECESSARY : If there are decimal places , Just throw it ArithmeticException abnormal .
Set separately
BigDecimal a = new BigDecimal("998.5");
BigDecimal up = a.setScale(0, RoundingMode.UP);
System.out.println( "up ======= " + up);
BigDecimal down = a.setScale(0, RoundingMode.DOWN);
System.out.println( "down ======= " + down);
// Take the nearest larger integer
BigDecimal ceiling = a.setScale(0, RoundingMode.CEILING);
System.out.println( "ceiling ======= " + ceiling);
// Take the nearest smaller integer
BigDecimal floor = a.setScale(0, RoundingMode.FLOOR);
System.out.println( "floor ======= " + floor);
// rounding ,0.5 Rounding up
BigDecimal halfUp = a.setScale(0, RoundingMode.HALF_UP);
System.out.println( "halfUp ======= " + halfUp);
// rounding ,0.5 Rounding down
BigDecimal halfDown = a.setScale(0, RoundingMode.HALF_DOWN);
System.out.println( "halfDown ======= " + halfDown);
Division application
// Two decimal places are reserved for the result
BigDecimal divide = a.divide(b, 2, RoundingMode.UP);
System.out.println(divide);
边栏推荐
- oracle分组取最大值
- The most detailed summary of common English terms in the chip industry (quick grasp of graphics and text)
- JS global function method module exports exports
- 如果某个表有近千万数据,CRUD比较慢,如何优化
- node-red与TDengine交互
- mysql查询条件字段值末尾有空格也能查到数据问题
- Tool function: pay the non empty field value in one workspace to the same field in another workspace
- 闻泰科技收购安世半导体剩余股权获得无条件通过
- How to clearly understand and express IAAs, PAAS and SaaS?
- 逻辑回归原理
猜你喜欢

Analysis and recurrence of network security vulnerabilities

文件系统挂载

Go 语言变量

Six relationships of UML class diagram, the best way to learn and understand

Matlab drawing - points and vectors: method and source code of vector addition and subtraction

node-red与TDengine交互
![论文赏析[ICLR18]联合句法和词汇学习的神经语言模型](/img/1c/5b9726b16f67dfc2016a0c2035baae.png)
论文赏析[ICLR18]联合句法和词汇学习的神经语言模型

EWM收货ECC交货单校验逻辑问题

Redis cache penetration breakdown and avalanche

Use of postman
随机推荐
【STM32】看门狗模块
Swoole定时器
Recommendation system model: wide & deep model
Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始
Redis缓存穿透击穿和雪崩
美光起诉联电窃密案宣判:联电被罚1亿元新台币,三名员工被判刑!
[300 opencv routines] 241. Scale invariant feature transformation (SIFT)
Hierarchy of file system
力挺吴雄昂!Arm中国管理层发公开信:对莫须有的指控感到震惊和愤怒!
Brief analysis of advantages, disadvantages and development of SAP modules
mysql-JPA对数据库中JSON类型数据的支持
Redis-哨兵模式
Circular structure of shell system learning
Process and process scheduling
范德蒙德卷积 学习笔记
重新定义分析 - EventBridge 实时事件分析平台发布
uni-app进阶之样式框架/生产环境
Swoole websocket service
Six relationships of UML class diagram, the best way to learn and understand
数据库故障容错之系统时钟故障