What is Unity Mathf.Round?


Hello friends, today I want to talk to you about a number rounding function that is useful when making games in Unity. Our function is "Mathf.Round()".

In my previous post, I talked to you about the Mathf.Ceil function, and now I will talk about Round.
Let me first mention the difference between them before we start.

Ceil rounds up to the higher number, while Round tries to make it an integer by taking 5 as the threshold.

What is Mathf.Round?

Mathf.Round is a number rounding function and performs rounding operations by taking 5 as the threshold.
While it rounds values between 0 - 5 (including 5) down, it rounds parts greater than 5 up to the next integer.
Let's understand better with code and outputs.
void round(){
Debug.Log(Mathf.round(10.0)); // Will output 10
 Debug.Log(Mathf.round(10.2)); // Will output 10
 Debug.Log(Mathf.round(10.7)); // Will output 11
 Debug.Log(Mathf.round(-10.0)); // Will output -10
 Debug.Log(Mathf.round(-10.2)); // Will output -10
 Debug.Log(Mathf.round(-10.7)); // Will output -11
 }

How to Use Mathf.Round?
Actually, you may have more or less understood how it is used above, but let's still explain. To use it, first we write our code as "Mathf.Round("float-double number")" and call it where we want to use it.

 float float_number = 5.7f;
  int integer = Mathf.round(float_number); // Will output 6.