Math.Round() not working properly
Round(17.675, 2) is getting 17.68
Round(6.675, 2) also getting 6.68
But Round(36.675, 2) is getting 36.67
same like Round(38.675, 2) is getting 38.67
but when I Round(28.675, 2) is getting 28.68
I used all optional parameters, then also getting same result
How to resolve this ? pls help me
You can use the Round(Double, Int32, MidpointRounding) overload and specify the
MidpointRoundingparameter to get the desired rounding strategy.MidpointRoundingis an enum with these values (from the original documentation):The default is
MidpointRounding.ToEven. This is why it rounds sometimes up and sometime down. I.e., it round the midpoint values to the nearest even whole number.Also note that
doublevalues might not be what they appear to be. E.g.36.675could be stored as36.67499999999999. If you need a more reliable rounding, e.g. for money calculations, usedecimalinstead. The documentation for the System.Decimal struct says: