服务器之家

服务器之家 > 正文

关于C# Math 处理奇进偶不进的实现代码

时间:2021-11-21 15:45     来源/作者:C#教程网

话说,最近一次系统维护 用JS读取导入Excel中的实验数据,出现被自动四舍五入。后来到客户现场听客户反馈  Excel实验数据要求 奇进偶不进。

关于 奇进偶不进 产生的由来:从统计学的角度,“奇进偶舍”比“四舍五入”要科学,在大量运算时,它使舍入后的结果误差的均值趋于零,而不是像四舍五入那样逢五就入,导致结果偏向大数,使得误差产生积累进而产生系统误差,“奇进偶舍”使测量结果受到舍入误差的影响降到最低。

Math下找了下,使用Round 的重载,使用 MidpointRounding.ToEven 就可以实现 奇进偶不进。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// 4
      double d = 5.214;
      double res = Math.Round(d, 2, MidpointRounding.ToEven);
      Console.WriteLine(res);//5.21
 
      //6
      d = 5.216;
      res = Math.Round(d, 2, MidpointRounding.ToEven);
      Console.WriteLine(res);//5.22
 
      //5
      d = 5.215;
      res = Math.Round(d, 2, MidpointRounding.ToEven);
      Console.WriteLine(res);//5.22
      d = 5.225;
      res = Math.Round(d, 2, MidpointRounding.ToEven);
      Console.WriteLine(res);//5.22
 
 
 
 
      //不止小数点后3位时
      d = 0.7865666;
      res = Math.Round(d, 2, MidpointRounding.ToEven);
      Console.WriteLine(res);//0.79
 
      d = 0.786;
      res = Math.Round(d, 2, MidpointRounding.ToEven);
      Console.WriteLine(res);//0.79
 
      d = 0.785;
      res = Math.Round(d, 2, MidpointRounding.ToEven);
      Console.WriteLine(res);//0.78

以上这篇关于C# Math 处理奇进偶不进的实现代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部