HOWTO · Csharp

Convert decimal to int in C#

Choose truncation, nearest rounding, or directed rounding when converting a decimal to an int in C#, and handle values outside the Int32 range.

On this page

Use (int)value when you want to discard the fractional part. Use Convert.ToInt32(value) for nearest-integer rounding, and use a Math method when your rule is directed or has a specific midpoint policy. The following compact example compares these choices with positive values.

using System;

class Program
{
    static void Main()
    {
        decimal value = 12.9m;
        Console.WriteLine("cast: " + (int)value);
        Console.WriteLine("nearest: " + Convert.ToInt32(12.5m) + ", " + Convert.ToInt32(13.5m));
        Console.WriteLine("floor: " + Math.Floor(value));
        Console.WriteLine("ceiling: " + Math.Ceiling(value));
        Console.WriteLine("away: " + Math.Round(2.5m, MidpointRounding.AwayFromZero));
    }
}
cast: 12
nearest: 12, 14
floor: 12
ceiling: 13
away: 3

The conversion policy is a business rule, not a formatting detail. If the fractional part remains meaningful, such as in money or measurements, keep the value as decimal. Any final conversion to int must also account for values that do not fit in Int32.

Truncate a decimal toward zero

An explicit cast, (int)value, is the clearest choice for truncation. A decimal literal needs the m suffix; without it, a literal such as 12.9 is a double. Converting a decimal to an integral type rounds toward zero: (int)12.9m is 12, while (int)-12.9m is -12. This is neither floor nor ceiling. The C# numeric-conversions reference documents the toward-zero behavior and range failure.

Round to the nearest integer

Convert.ToInt32(decimal) rounds to the nearest 32-bit signed integer rather than truncating. When the fractional part is exactly .5, it uses midpoint-to-even rounding: Convert.ToInt32(12.5m) is 12, whereas Convert.ToInt32(13.5m) is 14. This is useful for measurements that should reach the nearest whole unit, but it is not the right policy when every midpoint must move away from zero.

The decimal overload documentation for Convert.ToInt32 specifies OverflowException for values outside the int range. The method does not make an oversized decimal safe.

Choose an explicit rounding rule

Apply a Math method to the decimal first, then convert the integral result. Math.Floor moves toward negative infinity, so Math.Floor(-12.9m) is -13m; Math.Ceiling moves toward positive infinity. Both therefore differ from (int)-12.9m, which is -12.

For nearest rounding with a deliberate tie rule, use Math.Round(value, MidpointRounding.AwayFromZero). It makes 2.5m become 3m and -2.5m become -3m. The default Math.Round(decimal) uses midpoint-to-even. Math.Truncate is another readable way to obtain a toward-zero integral decimal before converting it to int.

Do not cast before rounding: (int)value has already discarded the fraction, so a later call to Math.Round cannot restore the intended choice. Round first, check the result, and then cast.

Handle values outside the int range

An int is a 32-bit signed integer bounded by int.MinValue and int.MaxValue; Int32 is the corresponding framework type. A decimal can represent values beyond those bounds. A decimal-to-int conversion already throws OverflowException when the resulting value is outside that range; it does not clamp the value to an endpoint.

Validate a value before conversion when an out-of-range input is possible, or catch OverflowException at the application boundary. A checked conversion can make the intent explicit and can help test the range contract, but it is not the mechanism that detects overflow for a decimal-to-int conversion. When the receiving contract permits it, retain decimal or choose a wider integer type instead of silently forcing the value into int.

Pick the conversion that matches the rule

Intended result Use Important behavior
Discard the fraction (int)value Truncates toward zero.
Nearest int Convert.ToInt32(value) Midpoints round to even.
Always lower or higher Math.Floor or Math.Ceiling, then cast Directed rounding differs from truncation for negative values.
Custom midpoint behavior Math.Round with MidpointRounding, then cast State the tie-breaking rule explicitly.

Choose the explicit cast for ordinary truncation. Choose Convert.ToInt32 or a Math method only when its rounding policy is the one your application requires, and handle OverflowException at the int boundary.