.NET Math.Round() #EPICFAIL

Consider this: you have a number, say 392.865, and you want to round that number to two decimal places. The resulting value should be 392.87, right?

Well… well, yeah… let me just gather my breath for a second here…

If you’re using the .NET framework’s Math.Round() method then boy do I have a surprise for you! The result of Math.Round(392.865, 2) will be 392.86.

This is because .NET uses “banker’s rounding” by default.

You think I’m joking don’t you?

I should be joking, but I’m not… see MSDN for confirmation.

I’ve been writing .NET code for 11 years and I never knew about this until today. I wonder how many calculations (not just mine, but everyone’s around the world) have been and will continue to be incorrect because of this default behavior?

You have been warned.

#MindBlown

Comment are closed.