当前位置:网站首页>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
边栏推荐
- Vs2013 generate solutions super slow solutions
- H5 web player easyplayer How does JS realize live video real-time recording?
- ViewPager2和VIewPager的區別以及ViewPager2實現輪播圖
- 小程序滑动、点击切换简洁UI
- H5网页播放器EasyPlayer.js如何实现直播视频实时录像?
- 20排位赛3
- [bw16 application] Anxin can realize mqtt communication with bw16 module / development board at instruction
- 【BW16 应用篇】安信可BW16模组/开发板AT指令实现MQTT通讯
- 4、 Fundamentals of machine learning
- csdn涨薪技术-浅学Jmeter的几个常用的逻辑控制器使用
猜你喜欢
随机推荐
数据库多表关联查询问题
创建一个长度为6的int型数组,要求数组元素的值都在1-30之间,且是随机赋值。同时,要求元素的值各不相同。
20排位赛3
csdn涨薪技术-浅学Jmeter的几个常用的逻辑控制器使用
Windows starts redis service
iNFTnews | 时尚品牌将以什么方式进入元宇宙?
Software modeling and analysis
Unity shader (data type in cghlsl)
JS judge whether checkbox is selected in the project
【无标题】
Sqlplus garbled code problem, find the solution
The industrial chain of consumer Internet is actually very short. It only undertakes the role of docking and matchmaking between upstream and downstream platforms
战略合作|SubQuery 成为章鱼网络浏览器的秘密武器
**Grafana installation**
Lesson 1: hardness of eggs
How will fashion brands enter the meta universe?
thinkphp3.2信息泄露
Over 100000 words_ Ultra detailed SSM integration practice_ Manually implement permission management
4、 Fundamentals of machine learning
如何使用clipboard.js库实现复制剪切功能