简体   繁体   中英

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 ?

Third digit is 5 so we check for 4th digit that is 1 less than 5 so result should be 27.23

Is there any way to get this result.

See My problem:

I have amount 2013.86, and tax 7%. Now 7% of 2013.86 = 140.97

But this 2013.86 is my total amount.

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.

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.

The midpoint rounding option only comes into play when a number is exactly halfway between the two choices, ie in your case 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.

You should also not use double for currency calculations - use decimal instead. Always suffix the number with an m , ie 27.2351m rather than 27.2351 (which is a 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.

So this is the call you need:

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

That gives 140.98m .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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