Address
304 North Cardinal St.
Dorchester Center, MA 02124

Work Hours
Monday to Friday: 7AM - 7PM
Weekend: 10AM - 5PM

Unity float to int

Converting Float to Int in Unity

In Unity, converting a floating-point number (float) to an integer (int) is a common operation and can be done in several ways.

This article will guide you through the different functions to use for this conversion depending on your use case.

Table of Contents

Converting to the Smallest Integer

To convert a float to the smallest integer, you have two options: you can either perform a direct cast to int or use the “ToInt32” method from the Convert class.

Depending on your needs, you can also use “ToInt64” or “ToInt16”.

using System;
using UnityEngine;

public class ConvertBehaviour : MonoBehaviour
{
    public int CastConvert(float value)
    {
        return (int)value;
    }

    public int SystemConvert(float value)
    {
        return Convert.ToInt32(value);
    }

    private void Start()
    {
        Debug.Log(CastConvert(0.5f)); // 0
        Debug.Log(CastConvert(0.6f)); // 0
        Debug.Log(CastConvert(2f));   // 2
        Debug.Log(CastConvert(2.1f)); // 2
    }
}
    

Converting to the Largest Integer

To convert a float to the largest integer, you can use the CeilToInt method from the Mathf class.

using UnityEngine;

public class ConvertBehaviour : MonoBehaviour
{
    public int CeilToIntConvert(float value)
    {
        return Mathf.CeilToInt(value);
    }

    private void Start()
    {
        Debug.Log(CeilToIntConvert(0.5f)); // 1
        Debug.Log(CeilToIntConvert(0.6f)); // 1
        Debug.Log(CeilToIntConvert(2f));   // 2
        Debug.Log(CeilToIntConvert(2.1f)); // 3
    }
}
    

Converting to the Nearest Integer

To convert a float to the nearest integer, you can use the RoundToInt method from the Mathf class.

Be careful, if the number ends in .5 so it is halfway between two integers, one of which is even and the other odd, the even number is returned.

using UnityEngine;

public class ConvertBehaviour : MonoBehaviour
{
    public int RoundConvert(float value)
    {
        return Mathf.RoundToInt(value);
    }

    private void Start()
    {
        Debug.Log(RoundConvert(0.5f)); // 0
        Debug.Log(RoundConvert(0.6f)); // 1
        Debug.Log(RoundConvert(2f));   // 2
        Debug.Log(RoundConvert(2.1f)); // 2
        Debug.Log(RoundConvert(7.5f)); // 8
    }
}
    

Converting to Int for String Usage

To convert a float to an int for use as a string, you can simply call the ToString method and specify the format “0”.

Be aware that this will work like the round function, except that when the number is halfway between the two integers, it will always return the larger one.

Check the documentation: The “0” custom specifier

using UnityEngine;

public class ConvertBehaviour : MonoBehaviour
{
    public string ToStringConvert(float value)
    {
        return value.ToString("0");
    }

    private void Start()
    {
        Debug.Log(ToStringConvert(0.5f)); // "1"
        Debug.Log(ToStringConvert(0.6f)); // "1"
        Debug.Log(ToStringConvert(2f));   // "2"
        Debug.Log(ToStringConvert(2.1f)); // "2"
    }
}
    

I also leave you the complete documentation on ToString formats.

If you’re still short of use cases, you might want to turn to the “Math” class, which has a “Round” method with more parameters than Unity’s.

It is also possible to perform the reverse operation: int to float

For further learning, here are other articles on type casting:

Learn more

Have you just started your Unity adventure?
Check this article to start in the best possible way: The Best Advice I Wish I Had When I Started Game Development

Check our other articles about Unity:

Leave a Reply

Your email address will not be published. Required fields are marked *