• While working with payments, it is important to match till even a one cent. As it says “Every little step counts”, 1 cent mismatch with many orders can lead us to a huge mismatch number.

    With our huge Youth Sports Client, I have been facing a similar situation in matching invoices. Thousands of payments processing on each tenant on the multi-tenant website and facing mismatches for some of the captured payments against the received amount at merchant accounts was a worrisome issue.

    While looking into all the payments and how 1 cent mismatch happens with some of them, I came to the conclusion that rounding off works differently for Asp.Net and SQL server.

    If you have $3.85

    SQL:  ROUND(3.85, 1) results in 3.9

    Asp.Net:  Math.Round(3.85, 1) results in 3.8


    Solution:

     

    The Asp.Net Math.Round method comes with an overload of defining the 3rd parameter.

    Method: Round(Double, Int32, MidpointRounding)

    The Round method supports two rounding conventions for handling midpoint values:

     

    Rounding away from zero

    Midpoint values are rounded to the next number away from zero. For example, 3.75 rounds to 3.8, 3.85 rounds to 3.9, -3.75 rounds to -3.8, and -3.85 rounds to -3.9. This form of rounding is represented by the MidpointRounding.AwayFromZero enumeration member.

     

    Rounding to nearest even, or banker's rounding

    Midpoint values are rounded to the nearest even number. For example, both 3.75 and 3.85 round to 3.8, and both -3.75 and -3.85 round to -3.8. This form of rounding is represented by the MidpointRounding.ToEven enumeration member.

     

    By default, Math.Round uses MidpointRounding.ToEven(Banker's round) which rounds to the nearest even number.


    ASP.Net Application Result

     

    Math.Round(3.85, 1) results in 3.8

    Math.Round(3.85, 1, MidpointRounding.AwayFromZero) results in 3.9

    SQL Server Query Result

0 Years in
Operation
0 Loyal
Clients
0 Successful
Projects

Words from our clients

 

Tell Us About Your Project

We’ve done lot’s of work, Let’s Check some from here