当前位置:网站首页>Pit using BigDecimal
Pit using BigDecimal
2022-07-07 09:46:00 【Keeling1720】
BigDecimal The pit of
BigDecimal It is often used by us to calculate some scenes that need accurate calculation , For example, calculation of amount . however ,BigDecimal There are also many unknown pits . below , Let's briefly introduce some common pits .
1、 Use valueOf() replace new BigDecimal
public static void main(String[] args) {
BigDecimal bigDecimal1 = new BigDecimal(0.01);
BigDecimal bigDecimal2 = BigDecimal.valueOf(0.01);
System.out.println(bigDecimal1);
System.out.println(bigDecimal2);
}
The output is :
You can see , direct new BigDecimal Pass in 0.01 When , because 0.01 This digital computer cannot accurately express , Cause it to be passed into BigDecimal Precision has been lost in . There is an error in the final output . and valueOf Is different , His bottom will Double Convert to String, Ensure that his accuracy will not be lost .
public static BigDecimal valueOf(double val) {
return new BigDecimal(Double.toString(val));
}
summary : Try to use string form to construct BigDecimal object , If it doesn't work , Then please also use BigDecimal.valueOf() Method to convert the value , To ensure our accuracy .
2、 Use compareTo instead of equals Compare
public static void main(String[] args) {
BigDecimal bigDecimal1 = new BigDecimal("1.0");
BigDecimal bigDecimal2 = new BigDecimal("1.00");
System.out.println(bigDecimal2.equals(bigDecimal1));
System.out.println(bigDecimal2.compareTo(bigDecimal1));
}
Running results :
Running results , Use equals Compare two BigDecimal The value of will be equal to false, because BigDecimal Of equals Will compare the accuracy of two numbers , and compareTo Method will only compare the size of two numbers .
summary : Use compareTo instead of equals Compare the two BigDecimal Value .
3、BigDecimal Not infinite precision
public static void main(String[] args) {
BigDecimal bigDecimal1 = new BigDecimal("1.0");
BigDecimal bigDecimal2 = new BigDecimal("3.0");
bigDecimal1.divide(bigDecimal2);
}
The operation results are as follows :
This is because ,1 / 3 be equal to Infinitely recurring decimals (0.33333… ). This is the time , We must tell JVM, We don't need the most accurate results . Change the code to :
public static void main(String[] args) {
BigDecimal bigDecimal1 = new BigDecimal("1.0");
BigDecimal bigDecimal2 = new BigDecimal("3.0");
System.out.println(bigDecimal1.divide(bigDecimal2, RoundingMode.HALF_UP));
}
Running results :
4、 Turn into String Use the right method
public static void main(String[] args) {
BigDecimal bigDecimal = BigDecimal.valueOf(12345678902132123113213.12345678912345678);
// When necessary, , Use scientific counting
System.out.println(bigDecimal.toString());
// No scientific counting
System.out.println(bigDecimal.toPlainString());
// The method of recording numbers often used in engineering calculation , It's like scientific counting , But the requirement is 10 The power of must be 3 Multiple
System.out.println(bigDecimal.toEngineeringString());
}
Running results :
summary :
- toString(): If necessary , Use scientific counting .
- toPlainString(): No scientific counting
- toEngineeringString(): The method of recording numbers often used in engineering calculation , It's like scientific counting , But the requirement is 10 The power of must be 3 Multiple
边栏推荐
- 如何成为一名高级数字 IC 设计工程师(5-3)理论篇:ULP 低功耗设计技术精讲(下)
- How to become a senior digital IC Design Engineer (5-3) theory: ULP low power design technology (Part 2)
- 如何使用clipboard.js库实现复制剪切功能
- Sqlplus garbled code problem, find the solution
- Using JWT to realize login function
- 2020CCPC威海 J - Steins;Game (sg函数、线性基)
- 小程序滑动、点击切换简洁UI
- 数据库多表关联查询问题
- Oracle安装增强功能出错
- Netease cloud wechat applet
猜你喜欢
[Frida practice] "one line" code teaches you to obtain all Lua scripts in wegame platform
【frida实战】“一行”代码教你获取WeGame平台中所有的lua脚本
nlohmann json
【无标题】
Oracle installation enhancements error
12、 Sort
In fact, it's very simple. It teaches you to easily realize the cool data visualization big screen
Use 3 in data modeling σ Eliminate outliers for data cleaning
AI从感知走向智能认知
VSCode+mingw64
随机推荐
Information Security Experiment 2: using x-scanner scanning tool
用flinksql的方式 写进 sr的表,发现需要删除的数据没有删除,参照文档https://do
Elaborate on MySQL mvcc multi version control
IIS faked death this morning, various troubleshooting, has been solved
2020CCPC威海 J - Steins;Game (sg函数、线性基)
Kubernetes cluster capacity expansion to add node nodes
在EXCEL写VBA连接ORACLE并查询数据库中的内容
CDZSC_2022寒假个人训练赛21级(1)
How to become a senior digital IC Design Engineer (5-2) theory: ULP low power design technology (Part 1)
Thinkphp3.2 information disclosure
CSDN salary increase technology - learn about the use of several common logic controllers of JMeter
H5网页播放器EasyPlayer.js如何实现直播视频实时录像?
Can flycdc use SqlClient to specify mysqlbinlog ID to execute tasks
创建一个长度为6的int型数组,要求数组元素的值都在1-30之间,且是随机赋值。同时,要求元素的值各不相同。
数据库多表关联查询问题
Dynamics 365Online ApplicationUser创建方式变更
剑指 Offer II 107. 矩阵中的距离
Communication mode between processes
js逆向教程第二发-猿人学第一题
**Grafana installation**