简体   繁体   English

如何使用NET Math.Round( <decimal> , <int> ,MidpointRounding.AwayFromZero)

[英]How to round off correctly with NET Math.Round(<decimal>,<int>,MidpointRounding.AwayFromZero)

In .NET, why does System.Math.Round(27.2351, 2, MidpointRounding.AwayFromZero) yield 27.24 instead of 27.23 ? 在.NET中,为什么System.Math.Round(27.2351, 2, MidpointRounding.AwayFromZero)产生27.24而不是27.23

Third digit is 5 so we check for 4th digit that is 1 less than 5 so result should be 27.23 第三位数字为5,因此我们检查第四位数字是否小于5,因此结果应为27.23

Is there any way to get this result. 有什么办法可以得到这个结果。

See My problem: 请参阅我的问题:

I have amount 2013.86, and tax 7%. 我的金额为2013.86,税金为7%。 Now 7% of 2013.86 = 140.97 现在是2013.86的7%= 140.97

But this 2013.86 is my total amount. 但是这2013.86是我的总额。

details are like this: 详细信息如下:

Amount     Tax     Tax Value in 2 decimals by Math.Round
718.37      7%      50.29
496.06      7%      34.72
384.70      7%      26.93
310.93      7%      21.77
103.80      7%      7.27
----------------------------
2013.86             140.98

Total amount tax is 140.97 but inidividual's is 140.98 :( :( How to match total amount tax to summation of individual amount's tax. 总税额为140.97,而个人税额为140.98 :( :(如何将总税额与个人税额的总和相匹配。

Any Suggestion 任何建议

Your understanding of rounding is incorrect. 您对四舍五入的理解是不正确的。 27.2351 is closer to 27.24 than 27.23, and therefore rounding 27.2351 to 2 decimal places will always produce 27.24 regardless of any midpoint rounding option. 27.2351比27.23更接近27.24,因此,不管有任何中点舍入选项,将27.2351舍入到小数点后两位将始终产生27.24。

The midpoint rounding option only comes into play when a number is exactly halfway between the two choices, ie in your case 27.2350000. 中点舍入选项仅在数字恰好位于两个选项之间的中间位置时才起作用,即您的情况是27.2350000。 Consider the number line: 考虑数字行:

/--What to do at this exact point?
                                        |  It's not "closer" to either side!
                                        |
                                        v
.............round to 27.230............|...........round to 27.240............
<~---------------------------------------------------------------------------~>
      |                                 |                                 |
    27.230                            27.235                            27.240

As for the behavior you're experiencing in your edit, this is normal. 至于您在编辑中遇到的行为,这是正常的。 The sum of rounded numbers is not always going to be the same as if you sum the numbers first, then round once. 四舍五入的数字总和不一定总是与先将数字相加然后再四舍五入相同。 You need to read up on how tax is supposed to be calculated for your particular jurisdiction, then follow that. 您需要阅读有关如何为您的特定辖区计算税金的信息,然后再执行此操作。

To start with the call to System.Math.Round(27.2351, 3, MidpointRounding.AwayFromZero) should return 27.24 as that's the correct answer to 2 decimal places. 首先调用System.Math.Round(27.2351, 3, MidpointRounding.AwayFromZero)应该返回27.24因为这是对2小数的正确答案。

You should also not use double for currency calculations - use decimal instead. 您也不应将double用于货币计算,而应使用decimal Always suffix the number with an m , ie 27.2351m rather than 27.2351 (which is a double ). 始终在数字后加上m ,即27.2351m而不是27.2351 (这是一个double )。

Now, in the rest of your question you appear to be talking about getting the "ceiling", and not rounding. 现在,在剩下的问题中,您似乎在谈论获得“天花板”,而不是四舍五入。

The call System.Math.Round(2013.86m * 0.07m, 2, MidpointRounding.AwayFromZero) gives you 140.97m , but the tax should always go up to the nearest cent if there is a fractional amount of cents. 调用System.Math.Round(2013.86m * 0.07m, 2, MidpointRounding.AwayFromZero)给您140.97m ,但是如果有小数美分,税金应始终升至最接近的美分。

So this is the call you need: 因此,这是您需要的电话:

System.Math.Ceiling(2013.86m * 0.07m * 100m) / 100m

That gives 140.98m . 得出140.98m

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

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