1

I am using a Math.Round function to round the decimal numbers with a precision of 3 digits, but for some equations it gives a wrong result.The code given below gives me a wrong result.It gives 1.428 but the expected result is 1.429

Math.Round(28.57 * 5.0 / 100, 3, MidpointRounding.AwayFromZero)

I got the right answer by just putting 5.0 / 100 in a bracket.The code given below gives 1.429

Math.Round(28.57 * (5.0 / 100), 3, MidpointRounding.AwayFromZero)

I don't understand why it is happening like this. Can you explain this?

1 Answers1

1

Usually, floating-point numbers may not have an "exact" representation for the number, so they loose precision.

For your example, if you tried to subtract the values in both expressions, you will get a very very small result.

enter image description here

Youssef13
  • 3,836
  • 3
  • 24
  • 41