C # Math.Round error with x.95

Just stumbled upon one strange problem:

Math.Round(113.065, 2, MidpointRounding.ToEven) => 113.06 Math.Round(113.075, 2, MidpointRounding.ToEven) => 113.08 Math.Round(113.085, 2, MidpointRounding.ToEven) => 113.08 

But...

 Math.Round(113.095, 2, MidpointRounding.ToEven) => 113.1 

Why is this happening? I want my result to be 113.09! Is it possible?

thanks

+4
source share
1 answer
 Math.Round(113.095, 2, MidpointRounding.ToEven) => 113.1 

It is even . Read it with two decimal places:

113.10

10 will be even. The trimmed right zero just makes it look odd (โ€œoddโ€ in the sense of โ€œnot evenโ€, not to be confused as โ€œoddโ€ meaning โ€œstrangeโ€), but thatโ€™s why it shows this path.

Also, your examples in your question are inaccurate. Here is the result from those that will further strengthen why you get x.1 as output:

 Math.Round(113.065, 2, MidpointRounding.ToEven) => 113.06 Math.Round(113.075, 2, MidpointRounding.ToEven) => 113.08 Math.Round(113.085, 2, MidpointRounding.ToEven) => 113.08 

Note that rounding 113.075 ToEven results in 113.0 8 , not 113.0 7 .

Full example:

 Math.Round(113.065, 2, MidpointRounding.ToEven) => 113.06 Math.Round(113.075, 2, MidpointRounding.ToEven) => 113.08 Math.Round(113.085, 2, MidpointRounding.ToEven) => 113.08 Math.Round(113.095, 2, MidpointRounding.ToEven) => 113.1 

Which can also be read as:

 Math.Round(113.065, 2, MidpointRounding.ToEven) => 113.06 Math.Round(113.075, 2, MidpointRounding.ToEven) => 113.08 Math.Round(113.085, 2, MidpointRounding.ToEven) => 113.08 Math.Round(113.095, 2, MidpointRounding.ToEven) => 113.10 

If the conclusion in your question is really what you want to get ... (i.e. if this is really your desired result, and you just want to know what you need to do to get this conclusion)

 Math.Floor(113.065 * 100) / 100 => 113.06 Math.Floor(113.075 * 100) / 100 => 113.07 Math.Floor(113.085 * 100) / 100 => 113.08 Math.Floor(113.095 * 100) / 100 => 113.09 
+11
source

All Articles