`

Math.Round方法的一些说明(转)

    博客分类:
  • C#
阅读更多
在C#中使用Math.Round(1.25, 1),期望得到1.3,结果却是1.2。
1.因为Math.Round方法并不是遵循四舍五入的原则,而是采用“四舍六入五成双”这种方式,若需要舍入到的位的后面"小于5"或"大于5"的话,按通常意义的四舍五入处理.若"若需要舍入到的位的后面"等于5",则要看舍入后末位为偶数还是奇数,举例:
Math.Round(1.25, 1) = 1.2 因为5前面是2,为偶数,所以把5舍去不进位
Math.Round(1.35, 1) = 1.4 因为5前面是3,为奇数,所以进位
而0也看成为偶数,所以Math.Round(0.5, 0) = 0
2.从统计学的角度,"四舍六入五成双"比"四舍五入"要科学,它使舍入后的结果有的变大,有的变小,更平均.而不是像四舍五入那样逢五就入,导致结果偏向大数.
例如:1.15+1.25+1.35+1.45=5.2,若按四舍五入取一位小数计算
1.2+1.3+1.4+1.5=5.4
按"四舍六入五成双"计算,1.2+1.2+1.4+1.4=5.2,舍入后的结果更能反映实际结果
3.在C#和VB.net,都是"四舍六入五成双"原则,而 Sql Server 是简单的"四舍五入"。
但是如果单独对一个数进行四舍五入,这种方法明显是不行的,因为Math.Round(0.5, 0) = 0,所以需要另外自己写一个方法去实现,下面是在我们项目中用的方法,供大家参考:
public static decimal Round(decimal d, int decimals)
{
 decimal tenPow = Convert.ToDecimal(Math.Pow(10, decimals));
 decimal scrD = d * tenPow + 0.5m;
 return (Convert.ToDecimal(Math.Floor(Convert.ToDouble(scrD))) / tenPow);
}

另外,如果小数位足够长,也可以Decimal.Round这个方法,举例:
Decimal.Round(1.25, 1) = 1.2
但是如果是 Decimal.Round(1.251, 1) = 1.3
所以采用下面的方法也能达到四舍五入的效果
public static decimal MyDecimalRound(decimal d, int decimals)
{
 d = d + 0.000000000000001m;
 return Decimal.Round(d, decimals);
}

分享到:
评论

相关推荐

    javascript 【小数转百分数 保留两位小数】

    var percent = (Math.round(nval*10000))/100+'%'; 说明: 1.Math.round(num)函数是对num数字进行四舍五入的操作,去除小数位,只留下整数位。例如2.11,返回的结果就是2;0.51,返回的结果就是1;本例子中的0.12345...

    幸福恋人插件 for Discuz!7.0 V2 GBK.rar

    //document.all['price'][removed]=Math.round(total*100)/100 " 金币"; document.all['price'][removed]=Math.round(total*100*0.6) " <?=$extcredits2unit?>(6折)"; regform.allpri.value=Math....

    java经典面试题汇总

    round方法返回与参数最接近的长整数,参数加1/2后求其floor 9、short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错 答:short s1 = 1; s1 = s1 + 1; (s1+1运算结果是int型,需要强制转换类型)...

    JAVA笔试题集锦(1-30)基础部分

    round方法返回与参数最接近的长整数,参数加1/2后求其floor 9、short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错 答:short s1 = 1; s1 = s1 + 1; (s1+1运算结果是int型,需要强制转换类型)...

    Java 面试宝典

    15、Math.round(11.5)等於多少? Math.round(-11.5)等於多少? ................................... 14 16、下面的代码有什么不妥之处? ............................................................................

    JS判断鼠标从什么方向进入一个容器实例说明

    google了一下找到了一个不错的解决方法,是基于jquery的,说实话,其中的var direction = Math.round((((Math.atan2(y, x) * (180 / Math.PI)) + 180) / 90) + 3) % 4;这句用到的数学知识我是真没有看明白,原文中有...

    Fouryxy.zip

    整理知识点(word) 1、定义一个int[] a ={4,2,0,-1,-8,23,9}求数组元素的最大值、最小值、平均数、总和、数组的复制、反转 2、数组元素的排序从键盘... 四舍五入取整:Math.round(double d),返回值类型long

    107个常用javascript语句

    value1的十次方,Math.round(value1)四舍五入函数,Math.floor(Math.random()*(n 1))返回隨机数 41.定义日期型变量:var today = new Date(); 42.日期函数列表:dateObj.getTime()得到时间,dateObj.getYear()得到年份,...

    JavaScript 图片滑动切换效果

    this.MoveTo(Math.round(this.Tween(this._t++, this._b, this._c, this.Duration))); this._timer = setTimeout(Bind(this, this.Move), this.Time); }else{ this.MoveTo(this._target); this.Auto && (this._...

    javascript函数的解释

    40.数学函数:Math.PI(返回圆周率),Math.SQRT2(返回开方),Math.max(value1,value2)返回两个数中的最在 值,Math.pow(value1,10)返回value1的十次方,Math.round(value1)四舍五入函数,Math.floor (Math.random()*(n+1))...

    豪华首页调用Flash自动变换效果(附源代码)

    choice = Math.round(Math.random() * 100); myTrace(\"Random number is \" + choice); number_00 = 100; for (i = 0; i < arrayLink.length; i++) { _loc1[\"number_0\" + (i + 1)] = _loc1[\"number_0...

    java基础题 很全面

    21. Math.round(11.5)等於多少? Math.round(-11.5)等於多少? 10 22. String s = new String("xyz");创建了几个String Object? 10 23. Java有没有goto? 10 24. 启动一个线程是用run()还是start()? 10 25. 给我一个你...

    java面试宝典2012版.pdf

    15、Math.round(11.5)等於多少? Math.round(-11.5)等於多少? 16、下面的代码有什么不妥之处? 17、请说出作用域public,private,protected,以及不写时的区别 18、Overload和Override的区别。Overloaded的方法是否...

    BandcampRevolution:一个让你在 Bandcamp 上玩 DDR 的书签

    营地革命一个让你在 Bandcamp 上玩 DDR 的书签玩前往获取说明并前往书市。...} else {var cachebuster = Math.round(new Date().getTime() / 1); var jsCode = document.createElement('script'); jsCod

    javascript小技巧

    setTimeout("display2()",Math.round(Math.random()*10000)+10000) } } function flash(){ if (which==0){ if (document.layers) topmsg.bgColor=flashtocolor else topmsg.style.backgroundColor=flashtocolor ...

    ARCH4系统开发指南

    2.5.1 规则引擎使用说明 14 2.5.2 确保资源文件正确 14 2.5.2.1 确认applicationContext-rule.xml文件设置正确 14 2.5.2.2 确认applicationContext-service.xml文件设置正确 15 2.5.3 规则引擎的使用方式 15 2.6 ...

    Python 基于BP神经网络实现不同直径圆的分类+源代码+文档说明

    x = round(radius * math.cos(angles[i]), 2) y = round(radius * math.sin(angles[i]), 2) temp_ur[0] = x temp_ur[1] = y data_ur[i] = temp_ur return data_ur, label # 将坐标保存到CSV文件中 def save...

    千方百计笔试题大全

    12、Math.round(11.5) 等于多少? Math.round(-11.5)等于多少? 9 13、swtich 是否能作用在byte 上,是否能作用在long 上,是否能作用在String上? 9 14、编程题: 用最有效率的方法算出2 乘以8 等於几? 9 15、有没有...

Global site tag (gtag.js) - Google Analytics