Quantcast
Viewing all articles
Browse latest Browse all 1710

Math.Round() problem - inconsistent result

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

amountresult

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




Viewing all articles
Browse latest Browse all 1710

Trending Articles