Decimal.Round默认设置MidpointRounding [英] Decimal.Round default setting for MidpointRounding

查看:695
本文介绍了Decimal.Round默认设置MidpointRounding的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下适用:

  VAR圆润= Decimal.Round(7.635米,2);
//圆形:7.63
 

这对我来说,是错误的,意外的行为。我假设的值的圆形 7.64

要做到这一点,我可以做的:

  VAR圆润= Decimal.Round(7.635米,2,MidpointRounding.AwayFromZero);
//圆形:7.64
 

怎么能这样的不可以是默认的 Decimal.Round 的行为?任何好的理由?

解决方案
  

这怎么不是Decimal.Round的默认行为?任何好的   原因?

如果你看一下 Decimal.Round法(十进制)

  

该方法的行为遵循IEEE标准754,第4节。   这种舍入有时称为舍入到最接近的或   银行家的舍入。它最大限度地减少而导致的舍入误差   持续舍入在单一方向上的中点值。它是   相当于调用圆(十进制,MidpointRounding)方法   模式MidpointRounding.ToEven的说法。

The following applies:

var rounded = Decimal.Round(7.635m, 2);
//rounded: 7.63

This, to me, is wrong and unexpected behavior. I would assume the value of rounded to be 7.64.

To achieve this, I can do:

var rounded = Decimal.Round(7.635m, 2, MidpointRounding.AwayFromZero);
//rounded: 7.64

How can this not be the default behavior of Decimal.Round? Any good reason for this?

解决方案

How can this not be the default behavior of Decimal.Round? Any good reason for this?

If you look at the documentation of Decimal.Round Method (Decimal)

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. It is equivalent to calling the Round(Decimal, MidpointRounding) method with a mode argument of MidpointRounding.ToEven.

这篇关于Decimal.Round默认设置MidpointRounding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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