Home > Software design >  C# is half to even (banking rounding) equivalent to MidpointRounding.ToEven?
C# is half to even (banking rounding) equivalent to MidpointRounding.ToEven?

Time:04-03

Recently I needed to implement "half to even" rounding method for an electronical documents.

I need to know if c# MidpointRounding.ToEven method of Math.Rounding function implements Half to even (Banking rounding) or if need to make it by myself .

CodePudding user response:

The documentation says exactly what you want:

ToEven: The strategy of rounding to the nearest number, and when a number is halfway between two others, it's rounded toward the nearest even number.

And then you can try it (https://dotnetfiddle.net/Mtly2u):

Math.Round(3.34, 1, MidpointRounding.ToEven) ==> 3.3
Math.Round(3.35, 1, MidpointRounding.ToEven) ==> 3.4
Math.Round(3.36, 1, MidpointRounding.ToEven) ==> 3.4
Math.Round(3.44, 1, MidpointRounding.ToEven) ==> 3.4
Math.Round(3.45, 1, MidpointRounding.ToEven) ==> 3.4
Math.Round(3.46, 1, MidpointRounding.ToEven) ==> 3.5
  •  Tags:  
  • c#
  • Related