HOWTO · Csharp

How to Convert Float to Int in C#

Convert a C# float to int with a direct cast, Math.Ceiling(), Math.Floor(), or Math.Round(), depending on the rounding behavior you need.

On this page

To convert a float to an int in C#, choose the operation based on how you want the fractional part handled: a direct cast truncates toward zero, Math.Ceiling() rounds toward positive infinity, Math.Floor() rounds toward negative infinity, and Math.Round() selects the nearest integer. Each conversion produces an int, and a fractional input can therefore change value.

Convert Float to Int With Explicit Typecasting in C#

A direct cast is the simplest option when you want to discard the fractional part. It truncates toward zero, so positive and negative values move toward zero rather than toward negative infinity. The following complete program converts both signs:

using System;

class Program
{
    static void Main()
    {
        float positive = 10.9f;
        float negative = -10.9f;

        int positiveResult = (int)positive;
        int negativeResult = (int)negative;

        Console.WriteLine(positiveResult);
        Console.WriteLine(negativeResult);
    }
}

The cast removes the fraction while preserving the direction toward zero: for example, 10.9f becomes 10, and -10.9f becomes -10. The result variables have type int. This example is supplied as code and has not been executed in this writing stage.

Convert Float to Int With Math.Ceiling() Function in C#

Use Math.Ceiling() when the value should be rounded toward positive infinity. It returns a double, so cast that result to int after calling the method. For a negative input, rounding toward positive infinity moves it closer to zero:

using System;

class Program
{
    static void Main()
    {
        float positive = 10.2f;
        float negative = -10.2f;

        int positiveResult = (int)Math.Ceiling(positive);
        int negativeResult = (int)Math.Ceiling(negative);

        Console.WriteLine(positiveResult);
        Console.WriteLine(negativeResult);
    }
}

Here, Math.Ceiling() rounds each value upward on the number line before the cast, and the cast stores the resulting whole number as an int. For example, the positive input becomes 11, while the negative input becomes -10. This example has not been executed in this writing stage.

Convert Float to Int With Math.Floor() Function in C#

Use Math.Floor() when the value should be rounded toward negative infinity. Like Math.Ceiling(), it returns a double; cast its result to int to store an integer:

using System;

class Program
{
    static void Main()
    {
        float positive = 10.2f;
        float negative = -10.2f;

        int positiveResult = (int)Math.Floor(positive);
        int negativeResult = (int)Math.Floor(negative);

        Console.WriteLine(positiveResult);
        Console.WriteLine(negativeResult);
    }
}

Rounding toward negative infinity gives 10 for the positive input and -11 for the negative input. This differs from a direct cast for negative fractions, because the cast moves toward zero. Both result variables are int. This example has not been executed in this writing stage.

Convert Float to Int With Math.Round() Function in C#

Use Math.Round() when you want the nearest integer. It returns a double, which can be cast to int. These inputs are not halfway values, so the example does not depend on a midpoint rounding rule:

using System;

class Program
{
    static void Main()
    {
        float positive = 10.8f;
        float negative = -10.8f;

        int positiveResult = (int)Math.Round(positive);
        int negativeResult = (int)Math.Round(negative);

        Console.WriteLine(positiveResult);
        Console.WriteLine(negativeResult);
    }
}

The nearest integers are 11 and -11, respectively. The fractional values do change during conversion; Math.Round() is useful when nearest-value rounding is the behavior your task requires, rather than as a universally preferable conversion. The results are int values. This example has not been executed in this writing stage.