1

When rounding I'm getting the following outputs:

Math.Round(2.3234503,4) = 2.3235

Math.Round(2.323450,4) = 2.3234

I can't see how this makes sense, can someone point it out to me? I expected an answer of 2.3235 both times. How can the number 3 or lack of in the last decimal place affect the outcome?

user48408
  • 3,234
  • 11
  • 39
  • 59
  • I don't believe it is, i know the default is to round to even instead of rounding away from zero but if you look again at the example ive shown here it demonstrates a different issue – user48408 Jan 30 '17 at 14:56
  • 1
    Because of the 03, you're not halfway anymore. You're slightly *above* halfway. – SuperOli Jan 30 '17 at 14:59
  • The question marked as duplicate is not relevant here. The value `2.323450` is *not* a tie, so it doesn't matter whether round-ties-to-even or round-ties-away-from-zero is in use. The value in the second example from the OP is slightly closer to `2.3234` than to `2.3235`, thanks to binary floating-point. (The actual value stored is `2.323449999999999793232063893810845911502838134765625`.) – Mark Dickinson Feb 01 '17 at 15:23
  • Better duplicate: http://stackoverflow.com/questions/16621832/math-round-bug-what-to-do – Mark Dickinson Feb 01 '17 at 15:30

1 Answers1

2

From MSDN

Returns:

The number nearest to d that contains a number of fractional digits equal to decimals.

Key word is nearest.

2.3234 503 - nearest to 503 is 5 so rounded to 5

2.3234 50 - is half way.

In half way from MSDN

In a midpoint value, the value after the least significant digit in the result is precisely half way between two numbers. For example, 3.47500 is a midpoint value if it is to be rounded two decimal places, and 7.500 is a midpoint value if it is to be rounded to an integer. In these cases, the nearest value can't be easily identified without a rounding convention. 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 away from zero is the most widely known form of rounding.

Rounding to nearest,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. Rounding to nearest is the standard form of rounding used in financial and statistical operations. It conforms to IEEE Standard 754, section 4. When used in multiple rounding operations, it reduces the rounding error that is caused by consistently rounding midpoint values in a single direction. In some cases, this rounding error can be significant.

By default, the Round method uses the rounding to nearest convention.

BWA
  • 5,672
  • 7
  • 34
  • 45
  • The OP is asking why the *half-point* number rounds down instead of up. That's not the *nearest* number. .NET is using round-to-even, often called Banker's Rounding. That's the IEEE 754 standard – Panagiotis Kanavos Jan 30 '17 at 15:04
  • @PanagiotisKanavos you are right, answer improved – BWA Jan 30 '17 at 15:11
  • Thanks for your input. My mistake what that I believed if you round to say 4 decimal places that you took the 5th number and applied the tie break rule. I did not know that you took all the remaining digits (i.e. 503) and applied the rule – user48408 Jan 31 '17 at 11:54
  • round-to-even is irrelevant here. The closest representable `double` value to `2.323450` is *not* a half-way case; it's slightly below the halfway value. – Mark Dickinson Feb 01 '17 at 15:25
  • You mean slightly above, right? – user48408 Feb 01 '17 at 15:30
  • @user48408: I'm fairly sure I mean below. :-) The exact value that's stored is `2.323449999999999793232063893810845911502838134765625`, which is sightly below the halfway value `2.32345` that acts as a threshold for the ties-to-even and ties-away-from-zero rounding methods. That's why it ends up rounding down. – Mark Dickinson Feb 01 '17 at 15:33