none
Math.Round() problem - inconsistent result RRS feed

  • General discussion

  • The problem I am having is that when I use the following Math.Round() code, it rounds up to the nearest whole number but that is not consistent with different input values.

    double result = 0;
    double amount = 20.955;
    result = Math.Round(amount, 2);
    Console.WriteLine(result);

    Result: 20.96

    if amount = 19.955 then the result is 19.95

    amount result

    7.955 7.96

    8.955 8.96

    12.955 12.96

    15.955 15.96

    16.955 16.95

    17.955 17.95

    18.955 18.95

    20.955 20.96

    27.955 27.96

    If you can closely observe the above values then you can find some inconsistency when rounding using Math.Round() method.

    Could someone let me know the algorithm it uses to compute the rounding?

    Thanks,

    Kishore



    • Edited by Kumar K S Monday, April 15, 2013 9:46 AM
    Monday, April 15, 2013 9:39 AM

All replies

  • "Could someone let me know the algorithm it uses to compute the rounding?"

    Math.Round(x, 2) is the same as Math.Round(x * 100.0) / 100.0.

    The source of the "problem" is that 100.0 which increases errors introduced by the fact that some floating point number that appear to have exact representation in base 10 do not have a equivalent representation in base 2. The following code should clear up thing:

                foreach (var d in new[] { 7.955, 8.955, 12.955, 15.955, 16.955, 17.955, 18.955, 20.955, 27.955 })
                    Console.WriteLine("{0:r} {1:r} {2:r}", d, d * 100.0, Math.Round(d * 100.0) / 100.0);
    

    Note how 16.955 becomes 1695.49999... rather than 1695.5. This otherwise very small error (something like 0.0000000000001) is then blown out by rounding and ends up as a 0.01 error.

    In general you should not expect "exact" results when using float/double. If those values represent currency (they look a bit like prices to me) then you should use decimal instead.

    Monday, April 15, 2013 10:10 AM
    Moderator