C#和Sql Server中的Math.Round [英] Math.Round in C# and Sql Server

查看:63
本文介绍了C#和Sql Server中的Math.Round的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#和sql server中的math.round是否相同.
我已经检查了以下内容,但我不了解Math.Round函数在C#中的工作方式.
Sql Server:
选择convert(varchar,convert(numeric(18,2),224.025000000))输出224.03

C#:
Math.Round(224.025000000))输出224.02
但是
Math.Round(2.345,2)输出2.35
Math.Round(2.245,2)输出2.24

谁能说出以上两个语句的输出为何不同?

在此先感谢.

Does math.round in C# and sql server are the same.
I have check the following, but I didn''t understand how the Math.Round function is working in C#.
Sql Server:
select convert(varchar,convert(numeric(18,2),224.025000000 )) Output 224.03

C#:
Math.Round(224.025000000)) Output 224.02
But
Math.Round(2.345, 2) Output 2.35
Math.Round(2.245, 2) Output 2.24

Can anybody tell Why the outputs of above two statements are different ?

Thanks in advance.

推荐答案

使用此
Math.Round(224.02500000, 2,MidpointRounding.AwayFromZero)


参考: http://msdn.microsoft.com/en-us/library/system.midpointrounding. aspx [^ ]


Ref:http://msdn.microsoft.com/en-us/library/system.midpointrounding.aspx[^]


从MSDN( http://msdn.microsoft.com /en-us/library/75ks3aby.aspx [ ^ ]):
给来电者的提示
由于将十进制值表示为浮点数或对浮点值执行算术运算可能会导致精度损失,因此在某些情况下,Round(Double,Int32)方法可能不会将中点值舍入到最接近的偶数小数位数后的值.在下面的示例中对此进行了说明,其中2.135舍入为2.13而不是2.14.发生这种情况是因为该方法在内部将值乘以10 *数字,并且这种情况下的乘法运算会损失精度.
From MSDN (http://msdn.microsoft.com/en-us/library/75ks3aby.aspx[^]):
Notes to Callers
Because of the loss of precision that can result from representing decimal values as floating-point numbers or performing arithmetic operations on floating-point values, in some cases the Round(Double, Int32) method may not appear to round midpoint values to the nearest even value in the digits decimal position. This is illustrated in the following example, where 2.135 is rounded to 2.13 instead of 2.14. This occurs because internally the method multiplies value by 10 * digits, and the multiplication operation in this case suffers from a loss of precision.


Hi Gopi,

请使用 ROUND(Transact-SQL)来实现.
Hi Gopi,

Please use ROUND (Transact-SQL) to achive this.


这篇关于C#和Sql Server中的Math.Round的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆