RoundUp给出错误的值 [英] RoundUp gives wrong value

查看:159
本文介绍了RoundUp给出错误的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All

我正在尝试使用 RoundUp()计算。我是使用以下代码。 

Double per =( Double )Convert.ToDecimal(( 1 .92 * 100 )/ 2 2 );
Double p = RoundUp(per, 2 );

给我一个p = 代码数字> 87
28
的code-sdkkeyword> value 87 27272722727277

原因 < span class =code-keyword>这个?当然应该返回 87 27 对吧?

请赐教。谢谢。

解决方案

嗨ErBhati,



我是猜你的 RoundUp 函数被创建为总是向上舍入 - 对我来说似乎合乎逻辑,似乎按预期工作,不是吗?无论如何,如果你不想总结,只需使用内置的

Math.Round()功能...



亲切的问候约翰内斯


如果RoundUp()函数的目的是...来舍入值UP ...那么它看起来像是工作正常吗?



如果你想要简化四舍五入的值,无论是向上还是向下,那么看看Round函数:

< pre lang =c#> Math.Round(每个, 2


阅读其中,特别是Jon Skeets回答



http://stackoverflow.com/questions/977796/why-does-math-round2-5-return-2-instead-of-3-in- c [ ^ ]



和这个MSDN



http://msdn.microsoft.com/en-us/library/ wyk4d9cy.aspx [ ^ ]



他们说是



Quote:



返回值

类型:System.Double

最接近a的整数。如果a的小数分量在两个整数之间,其中一个是偶数而另一个是奇数,则返回偶数。请注意,此方法返回Double而不是整数类型。


Hello All

  I am trying to calculate  using RoundUp(). I am using the following code.

  Double per =(Double)Convert.ToDecimal((1.92* 100) / 2.2);
  Double p = RoundUp(per, 2);

 It is giving me a value of p =87.28
 while value of per is 87.27272722727277

What is the reason for this? Surely it should be returning a value of 87.27 right?

Please enlighten me. Thanks.

解决方案

Hi ErBhati,

I'd guess your RoundUp function was created to "always round up" - seems logical for me, and seems to work as expected, no? Anyway if you want not "round up always" just use built-in
Math.Round() function...

Kind regards Johannes


If the purpose of the RoundUp() function is ... to round values UP ... then it looks like it's working correctly?

If you want your values simply rounded, either Up or Down, then have a look at the Round function:

Math.Round(per, 2)


Have a read of this, in particular Jon Skeets answer

http://stackoverflow.com/questions/977796/why-does-math-round2-5-return-2-instead-of-3-in-c[^]

and this MSDN

http://msdn.microsoft.com/en-us/library/wyk4d9cy.aspx[^]

They are saying that

Quote:


Return Value
Type: System.Double
The integer nearest a. If the fractional component of a is halfway between two integers, one of which is even and the other odd, then the even number is returned. Note that this method returns a Double instead of an integral type.


这篇关于RoundUp给出错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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