zoukankan      html  css  js  c++  java
  • 关于C# Math 处理奇进偶不进

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

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

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

                // 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

            

             

              

  • 相关阅读:
    鼠标不灵了,还好只是线的问题。自己DIY修下了
    [摘]编译MPlayer
    TPLINK路由器 硬重启方法
    Visual C++线程同步技术剖析 (转载)
    CListCtrl一行显示多个图标问题
    一位软件工程师的6年总结
    CCIE红头发讲解CCNA、CCNP视频教程
    图片链
    [摘]如何级联两个TPLINK路由器
    [摘]测试一下你对IP地址的掌握水平
  • 原文地址:https://www.cnblogs.com/lztkdr/p/MathRoundToEven.html
Copyright © 2011-2022 走看看