HOWTO · Csharp

How to Round a Double Value to an Integer Value in C#

Use Math.Ceiling(), Math.Floor(), or Math.Round() to round a double value to an integer value in C#, choosing the rule that matches your needs.

On this page

When converting a double to int, choose the rounding rule first. Use Math.Ceiling() to move toward positive infinity, Math.Floor() to move toward negative infinity, and Math.Round() to choose the nearest integral value. A cast such as (int)value truncates toward zero; it does not round.

The three math methods return an integral-valued double, so cast their result when an int is required. Check that the result is in the range of int before converting when inputs may be large.

Choose a Rounding Rule Before Converting to int

The direction matters for values on either side of zero. For example, Math.Ceiling(-8.3) moves to -8, while Math.Floor(-8.3) moves to -9. Neither method means simply “remove the fraction.” Use this comparison to select the operation that matches your application:

Operation Rule Example result for 8.3 Example result for -8.3
Math.Ceiling(value) Toward positive infinity 9 -8
Math.Floor(value) Toward negative infinity 8 -9
Math.Round(value) Nearest integer; midpoint ties go to even by default 8 -8
(int)value Truncate toward zero 8 -8

The table shows the result before conversion; each math method returns double. An explicit cast to int preserves these integral values for inputs within the integer range. The right choice depends on what the integer represents: a count, a bound, or a nearest estimate may each call for a different rule.

Run this self-contained example:

using System;

class Program
{
    static void Main()
    {
        double value = 8.3;
        double otherValue = -8.3;

        Console.WriteLine($"Ceiling: {(int)Math.Ceiling(value)}, {(int)Math.Ceiling(otherValue)}");
        Console.WriteLine($"Floor: {(int)Math.Floor(value)}, {(int)Math.Floor(otherValue)}");
        Console.WriteLine($"Nearest: {(int)Math.Round(value)}, {(int)Math.Round(otherValue)}");
        Console.WriteLine($"Cast: {(int)value}, {(int)otherValue}");
        Console.WriteLine($"Midpoints: {(int)Math.Round(2.5)}, {(int)Math.Round(3.5)}, {(int)Math.Round(2.5, MidpointRounding.AwayFromZero)}");
    }
}

Round Up with Math.Ceiling()

Math.Ceiling() returns the smallest integral-valued double greater than or equal to its input. This is rounding toward positive infinity: a positive fractional value moves to the next integer, while a negative fractional value moves to the integer closer to zero. Cast the result to int only after checking that it is representable as an int.

Use this rule when the result must never be less than the original value, such as determining how many whole containers are needed to hold a quantity. For example, Math.Ceiling(7.3) yields the integral-valued double 8; (int)Math.Ceiling(7.3) produces an integer for a suitable int range. If your requirement instead means “round away from zero,” Ceiling is not that rule for values below zero.

Round Down with Math.Floor()

Math.Floor() returns the largest integral-valued double less than or equal to its input. It rounds toward negative infinity. Thus Math.Floor(7.8) yields 7, and Math.Floor(-7.8) yields -8. This distinction is important: flooring a value below zero does not merely discard its fractional digits.

Use Math.Floor() when the result must not exceed the input, for example when computing the number of complete units in a measured quantity. As with Ceiling, convert the returned double to int only when the result fits the integer range. If you want to discard the fractional part in either direction toward zero, use a cast or Math.Truncate() instead.

Round to the Nearest Integer with Math.Round()

Math.Round() selects the nearest integral value and returns it as a double. Its default midpoint rule is to-even: when a value is exactly halfway between two integers, it chooses the even integer. For example, Math.Round(2.5) selects 2, while Math.Round(3.5) selects 4. This reduces directional bias across repeated midpoint calculations.

To choose the other common tie rule, pass MidpointRounding.AwayFromZero: Math.Round(2.5, MidpointRounding.AwayFromZero) selects 3, and Math.Round(-2.5, MidpointRounding.AwayFromZero) selects -3. This option changes midpoint handling; it does not make every value round away from zero. Cast the rounded result to int when needed and when it is in range.

Binary floating-point values can represent some decimal fractions only approximately. If a value is intended to be an exact midpoint but was calculated from other floating-point values, its stored value may lie slightly to one side of the midpoint. For rules that depend on exact decimal quantities, consider representing the domain with decimal and its corresponding Math.Round overload, while still selecting the desired midpoint mode.

Why a Cast Is Not Rounding

An explicit conversion from double to int truncates the fractional part toward zero. For example, (int)8.9 becomes 8, and (int)(-8.9) becomes -8. The second result differs from Math.Floor(-8.9), which is -9, and from nearest rounding, which chooses the closest integer under the selected midpoint rule.

Use a cast when truncation is the intended operation, not as a shortcut for “round to an integer.” If the input can lie outside the int range, handle that range explicitly before conversion; otherwise the result may not represent the value your application expects. In short, choose Math.Ceiling() for a bound toward positive infinity, Math.Floor() for a bound toward negative infinity, Math.Round() for the nearest integer, and a cast only for truncation toward zero.