当前位置:网站首页>The problem that double type cannot be accurately calculated
The problem that double type cannot be accurately calculated
2022-07-22 19:33:00 【scluis】
Preface
Recently, I was maintaining a C# project , Party a proposed a bug, as follows :
in other words , The user entered a number with two decimal places , But there was an error in our verification .
problem
The verification method of the original code in the project is as follows :
In the code, the user will enter decimal 100 Rounding times , And with this decimal 100 Times for comparison , If less than, it means that the user has entered more than two decimal places .
The idea is very simple , If the user enters less than two decimal places , So his 100 Times round and his 100 Times should be equal , conversely , It should be less than .
For example, user input 12.345, be money by 12.345, As expected ,centFloor by 1234, and cent by 1234.5,centFloor<cent, The user did enter more than decimals .
Good idea , But the reality is cruel , The problem lies in the use of double Calculate ,double As a double precision floating-point number , It is originally expressed in the form of base and index of several bits , It is not accurate , The result calculated with it is also more inaccurate . therefore It can only be used for storage , It cannot be used for calculation
Based on the input proposed by Party A 525.70 For example , The calculation results here are as follows :
We can see cent It's not what we expected 52570, But an exact decimal , such , Our judgment is in trouble .
In fact, as long as the number entered by the user is decimal , So his 100 Times and his 100 Times and then rounding cannot be equal !
I learned before Java When , Have you noticed this problem .
Java To solve double The method of calculating the problem is wrapper class BigDecimal, The code is as follows :
BigDecimal a=new BigDecimal(0.1);
BigDecimal b=new BigDecimal(0.2);
System.out.println(a.add(b));
BigDecimal c=new BigDecimal("0.1");
BigDecimal d=new BigDecimal("0.2");
System.out.println(c.add(d));
The results are as follows :
It should be noted that ,BigDecimal Initialization parameters cannot be used double Variable of type (Java The middle decimal is... By default double), To use String Type .
stay C# in , I found the use of decimal Type can support financial calculation with decimal .
The above double Switch to decimal after , Solved the problem :
But I still don't recommend this kind of writing , Regular expressions don't smell good ?
边栏推荐
- LeetCode 每日一题 2021/12/27-2022/1/2
- IT外包服务业各领域的未来前景和趋势
- Leetcode daily question 2022/2/28-2022/3/6
- 他的一只鸟,卖了6000万 ——明清大家八大山人藏品发售
- Force deduction solution summary 731- my schedule II
- JS advanced - lexical scope
- 【带你学c带你飞】第3章 分支结构(练习3.1 简单的猜数游戏)
- 字符集和字符编码
- Terminal data protection of Internet communication security
- Scenario practice | how to use rongyun super group to build a game community
猜你喜欢
Jackson parsing JSON detailed tutorial
An analysis of the CPU surge of an RFID tag management system in.Net
Industrial router oilfield wireless monitoring
grafana面板-覆盖字段值
Scope and lifecycle of beans
Swagger UI introduction and common notes
Batch check crawler
错过等一年!百度超级链数藏发行服务限时五折
Matlab plot子图的间距和边缘距离如何调整(已解决)
This points to the problem
随机推荐
【commons-beanutils专题】005- ConvertUtils 专题
Force deduction solution summary 522- longest special sequence II
什么是“实时”
Leetcode daily question 2021/11/29-2021/12/5
Leetcode: 1179. Reformat department table
About human resource outsourcing companies
4G工业路由器大气环境监测方案
Leetcode daily question 2021/12/6-2021/12/12
助力品牌洞察——消费者情绪行为分析
Why is it necessary to rewrite the hashcode method when rewriting the requests method
LeetCode 每日一题 2021/12/20-2021/12/26
“35岁,我退休了”:关于中年危机,这是最靠谱的回答
PHP implementation deletes a value in a one-dimensional array
为什么重写equels方法一定要重写hashCode方法
MySQL implements querying data from other tables and inserting another table
各种 XSS Payload 速查清单
Force deduction solution summary 498 diagonal traversal
Industrial router oilfield wireless monitoring
LeetCode 每日一题 2022/2/14-2022/2/20
Laravel solves [1045] access denied for user 'homestead' @ 'localhost' (usin g password: yes)