Rounding numbers ending with 5

I was expecting this video to mention how rounding numbers ending with 5 differed from what is thought in school.
If you do a normal Math.Round of for example 4.5 versus 3.5 you will get the same results because when the number ends with 5 it rounds to the nearest even number. 4.5 becomes 4 and 3.5 also rounds to 4.
This is what is expected for real world cases but if you want to round money you should not use Math.Round as is. For Money you will want to round numbers ending with 5 up.

You can experience this behavior with Excel, you will see different rounding depending if you set your cell data type to Accounting versus Number.

Thank you for sharing @zachbora.

There are a few different ways that you can round numbers that end in a 5.
As you mention, the most common are “AwayFromZero”, “ToEven”, but there are many more besides these two - each with their own pros and cons.

Different languages can have different default options available. However, the base Math classes in most of them will usually give you the option to specify which type of midpoint rounding you want to use.

I’d always recommend reading the documentation for your chosen language/game engine to see which options are available to you, so that you don’t get caught out by any weird rounding bugs.

1 Like

Privacy & Terms