简体   繁体   English

Excel中银行家的四舍五入公式

[英]Banker's rounding formula in Excel

It is a well known problem that by default, .NET uses Banker's rounding whereby X.5<\/code> is rounded to nearest even number.这是一个众所周知的问题,默认情况下,.NET 使用 Banker 的舍入,其中X.5<\/code>舍入到最接近的偶数。 Excel, on the other hand uses arithmetic rounding where X.5<\/code> is always rounded up, just like most of us have been taught at school.另一方面,Excel 使用算术舍入,其中X.5<\/code>总是四舍五入,就像我们大多数人在学校学习过的一样。

Is it possible to perform banker's rounding in Excel using a formula?是否可以使用公式在 Excel 中进行银行家四舍五入?

"

Use this formula:使用这个公式:

=IF(AND(ISEVEN(A1*10^0),MOD(A1*10^0,1)<=0.5),ROUNDDOWN(A1,0),ROUND(A1,0))

Replace all the 0 , there are 4 of them, with the significance of the desired rounding.替换所有0 ,其中有 4 个,具有所需舍入的意义。

Or you can create a user defined function to use VBA Round, which is Banker's Rounding:或者您可以创建一个用户定义的函数来使用 VBA Round,即 Banker's Rounding:

Function BankerRound(rng As Double, sig As Integer) As Double

    BankerRound = Round(rng, sig)
End Function

Then it would simply be:那么它只会是:

=BankerRound(A1,0)
=IF(AND(ISEVEN(A1*10^0),MOD(A1*10^0,1)<=0.5),ROUNDDOWN(A1,0),ROUND(A1,0))

I used this formula and it worked great, but in some cases, it was not working because the 0.5 actually was 0.5000000000000000004550000000000000000.我使用了这个公式,效果很好,但在某些情况下,它不起作用,因为 0.5 实际上是 0.5000000000000000004550000000000000000。

The fix is to ROUND the MOD statement to two(2) decimal places so that the computation reads:解决方法是将 MOD 语句四舍五入到小数点后两 (2) 位,以便计算结果如下:

=IF(AND(ISEVEN(A1*10^0),ROUND(MOD(A1*10^0,1),2)<=0.5),ROUNDDOWN(A1,0),ROUND(A1,0))

If for any reason the other codes don't work.如果由于任何原因其他代码不起作用。 This is a brute force variant of the ISEVEN types of formulas.这是 ISEVEN 类型公式的蛮力变体。

    =IF(AND(RIGHT(B29,1)="5", OR((RIGHT(LEFT(B29,LEN(B29)-1),1))="0", (RIGHT(LEFT(B29,LEN(B29)-1),1))="2", (RIGHT(LEFT(B29,LEN(B29)-1),1))="4", (RIGHT(LEFT(B29,LEN(B29)-1),1))="6", (RIGHT(LEFT(B29,LEN(B29)-1),1))="8")), ROUNDDOWN(B29,2), IF(AND(RIGHT(B29,1)="5", OR((RIGHT(LEFT(B29,LEN(B29)-1),1))="1", (RIGHT(LEFT(B29,LEN(B29)-1),1))="3", (RIGHT(LEFT(B29,LEN(B29)-1),1))="5", (RIGHT(LEFT(B29,LEN(B29)-1),1))="7", (RIGHT(LEFT(B29,LEN(B29)-1),1))="9")),ROUNDUP(B29,2), ROUND(B29,2)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM