为什么在C#Math.Round(2.5)返回2,而不是3? [英] Why does Math.Round(2.5) return 2 instead of 3 in C#?

查看:967
本文介绍了为什么在C#Math.Round(2.5)返回2,而不是3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中, Math.Round(2.5) 2的结果。

In C#, the result of Math.Round(2.5) is 2.

这应该是3,是不是?为什么是2,而不是在C#?

It is supposed to be 3, isn't it? Why is it 2 instead in C#?

推荐答案

首先,这将不会是反正一个C#的错误 - 这将是一个.NET错误。 C#是语言 - 它并不决定如何 Math.Round 实施

Firstly, this wouldn't be a C# bug anyway - it would be a .NET bug. C# is the language - it doesn't decide how Math.Round is implemented.

其次,没有 - 如果你看过的文档,你会看到,默认的舍入轮,甚至(银行家的舍入):

And secondly, no - if you read the docs, you'll see that the default rounding is "round to even" (banker's rounding):

返回值
类型:System.Double
整数最近的一个。如果   的小数部分的一半   两个整数,在它们之间的一个是   甚至与其他奇数,则甚至   号返回。注意,这   方法返回一个双击,而不是一个   整型。

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.

备注
该方法的行为遵循IEEE标准754,   第4节。这种舍是   有时也被称为四舍五入到最接近的,   或银行四舍五入。它最大限度地减少   舍入而导致的错误   持续舍入中点值   在单一方向上。

Remarks
The behavior of this method follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding to nearest, or banker's rounding. It minimizes rounding errors that result from consistently rounding a midpoint value in a single direction.

您可以指定 Math.Round 应该用圆中点的超载这需要一个<一个href="http://msdn.microsoft.com/en-us/library/system.midpointrounding.aspx"><$c$c>MidpointRounding值。还有一个过载与 MidpointRounding 对应于每个重载不具有之一:

You can specify how Math.Round should round mid-points using an overload which takes a MidpointRounding value. There's one overload with a MidpointRounding corresponding to each of the overloads which doesn't have one:

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

这是否默认情况下是很好的选择与否是另一回事。 ( MidpointRounding 只介绍了.NET 2.0。在那之前我不知道有实现所期望的行为,而不做自己的任何简单的方法。)特别是,历史已经表明,它不是的预计的行为 - 在大多数情况下,这是在API设计的一个大忌。我可以看到的为什么的银行家的四舍五入是非常有用的...但它仍然是一个惊喜很多。

Whether this default was well chosen or not is a different matter. (MidpointRounding was only introduced in .NET 2.0. Before then I'm not sure there was any easy way of implementing the desired behaviour without doing it yourself.) In particular, history has shown that it's not the expected behaviour - and in most cases that's a cardinal sin in API design. I can see why Banker's Rounding is useful... but it's still a surprise to many.

您可能有兴趣看看最近Java的等效枚举(<一href="http://java.sun.com/javase/6/docs/api/java/math/RoundingMode.html"><$c$c>RoundingMode)它提供了更多的选择。(它不只是处理中点。)

You may be interested to take a look at the nearest Java equivalent enum (RoundingMode) which offers even more options. (It doesn't just deal with midpoints.)

这篇关于为什么在C#Math.Round(2.5)返回2,而不是3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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