C#中Math.Round()实现中国式四舍五入

原文:C#中Math.Round()实现中国式四舍五入

C#中Math.Round()实现中国式四舍五入

 

C#中的Math.Round()并不是使用的"四舍五入"法。其实在VB、VBScript、C#、J#、T-SQL中Round函数都是采用Banker's rounding(银行家算法),即:四舍六入五取偶。事实上这也是IEEE的规范,因此所有符合IEEE标准的语言都应该采用这样的算法。

.NET 2.0 开始,Math.Round 方法提供了一个枚举选项 MidpointRounding.AwayFromZero 可以用来实现传统意义上的"四舍五入"。即: Math.Round(4.5, MidpointRounding.AwayFromZero) = 5。

Round(Decimal)
Round(Double)
Round(Decimal, Int32)
Round(Decimal, MidpointRounding)
Round(Double, Int32)
Round(Double, MidpointRounding)
Round(Decimal, Int32, MidpointRounding)
Round(Double, Int32, MidpointRounding)

 

 如:

Math.Round(0.4) //result:0

Math.Round(0.6) //result:1

Math.Round(0.5) //result:0

Math.Round(1.5) //result:2

Math.Round(2.5) //result:2

Math.Round(3.5) //result:4

Math.Round(4.5) //result:4

Math.Round(5.5) //result:6

Math.Round(6.5) //result:6

Math.Round(7.5) //result:8

Math.Round(8.5) //result:8

Math.Round(9.5) //result:10

   使用MidpointRounding.AwayFromZero重载后对比:   

Math.Round(0.4, MidpointRounding.AwayFromZero); // result:0

Math.Round(0.6, MidpointRounding.AwayFromZero); // result:1

Math.Round(0.5, MidpointRounding.AwayFromZero); // result:1

Math.Round(1.5, MidpointRounding.AwayFromZero); // result:2

Math.Round(2.5, MidpointRounding.AwayFromZero); // result:3

Math.Round(3.5, MidpointRounding.AwayFromZero); // result:4

Math.Round(4.5, MidpointRounding.AwayFromZero); // result:5

Math.Round(5.5, MidpointRounding.AwayFromZero); // result:6

Math.Round(6.5, MidpointRounding.AwayFromZero); // result:7

Math.Round(7.5, MidpointRounding.AwayFromZero); // result:8

Math.Round(8.5, MidpointRounding.AwayFromZero); // result:9

Math.Round(9.5, MidpointRounding.AwayFromZero); // result:10

   

   

但是悲剧的是,如果用这个计算小数的话,就不灵了!!!

必须用第七个重载方法,
decimal Round(decimal d, int decimals, MidpointRounding mode)

这样计算出来的小数才是真正的中国式四舍五入!!

  
 

?Math.Round(526.925, 2)
526.92

?Math.Round(526.925, 2,MidpointRounding.AwayFromZero)
526.92

?Math.Round((decimal)526.925, 2)
526.92

?Math.Round((decimal)526.925, 2,MidpointRounding.AwayFromZero)
526.93

 

  

时间: 2024-09-15 06:43:55

C#中Math.Round()实现中国式四舍五入的相关文章

Javascript Math.ceil()与Math.round()与Math.floor()区别

Math.ceil()向上舍入 1 2 3 alert(Math.ceil(20.1)) //输出 21 alert(Math.ceil(20.5)) //输出 21 alert(Math.ceil(20.9)) //输出 21  Math.round标准的四舍五入 1 2 3 alert(Math.round(20.1)) //输出 20 alert(Math.round(20.5)) //输出 21 alert(Math.round(20.9)) //输出 21  Math.floor()向

JavaScript中用于四舍五入的Math.round()方法讲解

  这篇文章主要介绍了JavaScript中用于四舍五入的Math.round()方法讲解,是JS入门学习中的基础知识,需要的朋友可以参考下 此方法返回一个数四舍五入为最接近的整数的值. 语法 ? 1 Math.round( x ) ; 下面是参数的详细信息: x: 一个数字 返回值: 返回数字四舍五入为最接近的整数的值. 例子: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <html> <head> <ti

JavaScript中用于四舍五入的Math.round()方法讲解_基础知识

 此方法返回一个数四舍五入为最接近的整数的值.语法 Math.round( x ) ; 下面是参数的详细信息:     x: 一个数字 返回值: 返回数字四舍五入为最接近的整数的值. 例子: <html> <head> <title>JavaScript Math round() Method</title> </head> <body> <script type="text/javascript">

JavaScipt中的Math.ceil() 、Math.floor() 、Math.round() 三个函数的理解_基础知识

首先还是看看JavaScript: The Definitive Guide, 4th Edition中对三个函数的定义. Math.ceil(): round a number up Arguments: Any numeric value or expression Returns: The closest integer greater than or equal to x. ---------------------------------------------------------

js中round()、oFixed()四舍五入函数

round方法 巨强的四舍五入的转换函数,如下:  代码如下 复制代码 function round(v,e){ var t=1; for(;e>0;t*=10,e--); for(;e<0;t/=10,e++); return Math.round(v*t)/t; } 参数里的: v表示要转换的值 e表示要保留的位数 函数里的两个for,这个是重点了, 第一个for针对小数点右边的情况,也就是保留小数点右边多少位: 第二个for针对小数点左边的情况,也就是保留小数点左边多少位. for的作用

js中Math之random,round,ceil,floor的用法总结_javascript技巧

<SPAN style="FONT-SIZE: 18px">1.Math.random(); 结果为0-1间的一个随机数(包括0,不包括1) </SPAN>  Math.random(); 结果为0-1间的一个随机数(包括0,不包括1) [html] view plaincopyprint?<SPAN style="FONT-SIZE: 18px">  2.Math.floor(num); 参数num为一个数值,函数结果为num的

Javascript四舍五入Math.round()与Math.pow()使用介绍_javascript技巧

复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Javascrip

JavaScript中Math对象方法使用概述

 JavaScript中Math对象方法如向上取整,有小数就整数部分加1,四舍五入.向下取整等等,下面为大家详细介绍下 1.丢弃小数部分,保留整数部分 parseInt(5/2)   2.向上取整,有小数就整数部分加1   Math.ceil(5/2)   3,四舍五入.   Math.round(5/2)   4,向下取整   Math.floor(5/2)   Math 对象的方法 FF: Firefox, N: Netscape, IE: Internet Explorer   方法 描述

C#里Math.Round()函数问题

Math.Round ()在四舍五入时有个问题: Math.Round(2.5,0) = 2; Math.Round(3.5,0) = 4; 2.5应该等于3才对! 在ASP中也存在这个问题,不过ASP中还有个FormatNumber可以用,但目前还不知道怎么使用? 解释: Math.Round()准确的说,这个函数不是四舍五入,而是四舍六入五凑偶,就是说小于4或大于6的该舍该入是没有争议的,而5处在正中间,如果四舍五入则会造成数据的整体偏差,所以采取的原则是:如果舍入位为5,则舍入后最后一位为