Where is the Mathf for round up when 5 or more and round down when 4 or less?

Question on where is the Mathf. for the normal day-to-day use round up behavior. Where you round up when 5 or more and round down when 4 or less? What do you call this? Is it call Round off?

All methods show below cannot do normal day-to-day rounding behavior.

forStudy1 = (int)2.5f; //print 2, this should print 3 in day-to-day usage.
forStudy2 = Mathf.RoundToInt(2.49f); //print 2
forStudy3 = Mathf.RoundToInt(2.50f); //print 2, this should print 3 in day-to-day usage.
forStudy4 = Mathf.RoundToInt(2.51f); //print 3

Did I miss something? What is your thought on this?

This thread is about Round down, round off, round up, and rounding numbers.

2 Likes

The default behavior of Mathf.Round() is called ‘bankers rounding,’ and if I said what I think about it, I am sure the thread would get embroiled into a pedantic argument.

If you want “day-to-day use round up behavior” then add 0.5f to your value and then use Floor() or FloorToInt().

4 Likes

Unity’s RoundToInt simply uses the System.Math.Round method which is round to the nearest even number. System.Math has an overload that takes a MidpointRounding as second argument and AwayFromZero would be the closest to what you want. However it’s not really the closest “larger” number as when on the negative side it will round down instead of up when exactly at .5. So adding 0.5 and using Floor is the usual approach.

3 Likes

Solved. by using MidpointRounding.AwayFromZero

Thank you spiney199 and **Bunny83 **for the Doc on MidpointRounding.

forStudyManualInput = -2.51f;
forStudy3 = Math.Round(forStudyManualInput, 0, MidpointRounding.AwayFromZero); // print -3
forStudyManualInput = -2.5f;
forStudy3 = Math.Round(forStudyManualInput, 0, MidpointRounding.AwayFromZero); // print -3
forStudyManualInput = -2.49f;
forStudy3 = Math.Round(forStudyManualInput, 0, MidpointRounding.AwayFromZero); // print -2
forStudyManualInput = 2.49f;
forStudy3 = Math.Round(forStudyManualInput, 0, MidpointRounding.AwayFromZero); // print 2
forStudyManualInput = 2.50f;
forStudy3 = Math.Round(forStudyManualInput, 0, MidpointRounding.AwayFromZero); // print 3
forStudyManualInput = 2.51f;
forStudy3 = Math.Round(forStudyManualInput, 0, MidpointRounding.AwayFromZero); // print 3

Keep in mind that the range around zero is smaller than all other ranges / slots as it goes from -0.49999... to 0.49999.... So both exact half points belong to either 1 or -1. So there’s a “slight” discrepancy in the range -1 to 1. When using Mathf.Floor(f+0.5f) you get a more consistent behaviour across the whole number line.

Though you barely should ever rely on the behaviour of how the exact midpoint is rounded. Floating point numbers always have rounding errors unless you stay in very very specific ranges, numbers and calculations. In most cases you can get slight errors. We round things so those errors get eliminated. However due to the errors you should never rely on having an exact value in the first place.

1 Like

These rounding concept is too complicated for me to understand at this moment, but I will keep that in mind. And Thank you for the tips.