How can I round a number?

My fallDamage gets returned as something like 10.2367844 depending on distance.

How can I round this up or down in script?

I could just say if var > 1 but < 2 = 1, but I'd have to do that right the way through to 100.

Is there an easier way?

Use Mathf.Round, Mathf.Ceil, Mathf.Floor, or cast them to ints:

int asInt = (int)floatValue;

For your example, you want to use Mathf.Floor or int casting, depending on your needs. Mathf.Floor return a float value. Casting returns of course an int value. Both approaches will get rid of any decimals.

You can use .Net functions in your scripts, take a look at Round function documentation:

Take a look at Mathf functions too:


// Prints 10