1
   public static decimal Round(
decimal d,
int decimals
    )

The decimals parameter specifies the number of fractional digits in the return value and ranges from 0 to 28. If decimals is zero, an integer is returned.

If the value of the first digit in d to the right of the decimals decimal position is 5, the digit in the decimals position is rounded up if it is odd, or left unchanged if it is even. If the precision of d is less than decimals, d is returned unchanged.

    Math.Round(3.44, 1); //Returns 3.4.
   Math.Round(3.45, 1); //Returns 3.4.

Why 3.45 is returning 3.4..I am unable to understand this output.Can anyone help

Amittai Shapira
  • 3,474
  • 28
  • 49
coder25
  • 2,069
  • 9
  • 46
  • 86

3 Answers3

4

You can change this behaviour by using the Round overload which take the MidpointRounding parameter, from MSDN:

ToEven (default, AKA Bankers Rounding) When a number is halfway between two others, it is rounded toward the nearest even number.
AwayFromZero When a number is halfway between two others, it is rounded toward the nearest number that is away from zero.

Amittai Shapira
  • 3,474
  • 28
  • 49
1

Like you said,

If the value of the first digit in d to the right of the decimals decimal position is 5, the digit in the decimals position is rounded up if it is odd, or left unchanged if it is even.

With 3.45, the first digit to the right of the decimal is 5, and since 4 is even, it's left unchanged. This is a pretty standard way of rounding, because if 5 is always rounded up, this can weight things like averages and sums higher than they should be.

Jodaka
  • 1,207
  • 8
  • 15
  • If you want to round 3.45 to 3.5 one would have to look at third decmial point. – Security Hound Oct 06 '11 at 14:05
  • first digit in d to the right of the decimals decimal position is 5,i am unable to understand this statement.....Its simple but i am unable to – coder25 Oct 06 '11 at 14:36
  • Round(1.55, 1) = 1.6 Round(1.65, 1) = 1.6 why this output is coming – coder25 Oct 06 '11 at 14:42
  • The integer you pass is the number of decimal places you're going to. When you pass 1, it means you want one decimal place after the decimal point. For 1.55 that's 5, for 1.65 that's 6, for 1.7234 that's 7, etc. When C# rounds (or when anything round, for that matter), it has to look at the digits after the decimals you want to know which way to round - otherwise, it would either have to truncate or randomly guess. When the digit after the decimals you want is a 5, it rounds to an even number as a way of removing bias from either always rounding down or always rounding up. – Jodaka Oct 06 '11 at 14:54
0

The default rounding is MidpointRounding.ToEven (banker's rounding) which means it will gravitate towards an even number for the digit at the rounding location (i.e., it will move to 3.4 because 4 is even).

This is intended to minimise the accumulation of errors which may occur when all midpoint rounding goes in the same direction (though this of course depends on your input data - an equal mix of positive and negative numbers may be fine with AwayFromZero).

So you have:

Math.Round(3.44, 1); //Returns 3.4.
Math.Round(3.45, 1); //Returns 3.4 (down towards 4).
Math.Round(3.54, 1); //Returns 3.5.
Math.Round(3.55, 1); //Returns 3.6 (up towards 6).

See this answer for a detailed explanation of all the options available to you.

Community
  • 1
  • 1
paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841