当前位置:网站首页>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);
边栏推荐
- 0-1 knapsack problem
- Redis-哨兵模式
- 6月19日上会,中芯国际或创造国内最快上市记录!
- C language programming | single dog topic explanation
- 学习笔记12:Eratosthenes筛选法求素数(100以内) 和 魔方阵
- Oracle error: ora-01722 invalid number
- Matlab 绘制 - 点和向量:向量加减的方法和源码
- Tear the source code of gateway by hand, and tear the source code of workflow and load balancing today
- The cooperation between starfish OS and metabell is just the beginning
- iperf安装与使用
猜你喜欢

iperf安装与使用
![Deepening the concept of linear algebra [23] 01 - points coordinate points and vectors vectors](/img/9e/267ab60455e859afac3727eff32803.png)
Deepening the concept of linear algebra [23] 01 - points coordinate points and vectors vectors

ABAP CDS Table Function介绍与示例

Redis缓存穿透击穿和雪崩

代码随想录笔记_哈希_1002查找共用字符

浏览器视频帧操作方法 requestVideoFrameCallback() 简介
![Thesis appreciation [iclr18] a neural language model combining syntax and vocabulary learning](/img/1c/5b9726b16f67dfc2016a0c2035baae.png)
Thesis appreciation [iclr18] a neural language model combining syntax and vocabulary learning

ABAP CDs table function introduction and examples

If asynchronous processing is implemented according to the framework
![论文赏析[ICLR18]联合句法和词汇学习的神经语言模型](/img/1c/5b9726b16f67dfc2016a0c2035baae.png)
论文赏析[ICLR18]联合句法和词汇学习的神经语言模型
随机推荐
Arm中国夺权大战的背后:“独立”两年,仍难“自主”?
Oracle error: ora-01722 invalid number
CAP的理解
SAP各模块优缺点和发展简析
Introduction to the browser video frame operation method requestvideoframecallback()
比亚迪半导体完成8亿元A+轮融资:30家知名投资机构入局,估值已达102亿元!
字节飞书人力资源套件三面
Swoole内存-table详解
浏览器视频帧操作方法 requestVideoFrameCallback() 简介
Tool function: pay the non empty field value in one workspace to the same field in another workspace
杂谈:一份最初就非常完善的FS跟第一版程序就要求没bug一样不切实际
Iperf installation and use
Basic learning of cesium
Thesis appreciation [iclr18] a neural language model combining syntax and vocabulary learning
Matlab 绘制 - 点和向量:向量加减的方法和源码
EWM收货ECC交货单校验逻辑问题
Sign up now | cloud native technology exchange meetup Guangzhou station has been opened, and I will meet you on August 6!
The cooperation between starfish OS and metabell is just the beginning
糟糕程序员的20个坏习惯
诺基亚宣布与博通合作开发5G芯片