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
To convert a C# decimal to an int, use an explicit cast when you deliberately want to discard the fractional part. Use Convert.ToInt32 when you want nearest-integer rounding instead. If the rule is always down, always up, or a particular midpoint policy, apply that rounding rule first and then cast. All of these conversions can throw OverflowException when the value does not fit in an Int32.
The choice is a business rule, not a formatting detail. For example, a quantity of -12.9m becomes -12 when truncated, but it becomes -13 when rounded down with Math.Floor. Keep the value as decimal if its fraction is still meaningful, such as for money or measurements.
Truncate a decimal toward zero
An explicit cast, (int)value, is the clearest choice when truncation is intended. A decimal literal needs the m suffix; without it, a literal such as 12.9 is a double instead. Converting a decimal to an integral type rounds toward zero, so the result is smaller for a positive fraction and larger for a negative fraction. It does not use floor or ceiling semantics. The C# numeric-conversions reference documents both the toward-zero behavior and the range failure.
using System;
decimal positive = 12.9m;
decimal negative = -12.9m;
Console.WriteLine($"cast: {(int)positive}, {(int)negative}");
Console.WriteLine($"round: {Convert.ToInt32(12.5m)}, {Convert.ToInt32(13.5m)}");
try
{
decimal tooLarge = decimal.MaxValue;
Console.WriteLine(checked((int)tooLarge));
}
catch (OverflowException)
{
Console.WriteLine("overflow");
}
cast: 12, -12
round: 12, 14
overflow
The first line shows the truncation rule: both fractional parts are discarded and the negative value moves toward zero. The last line is the boundary case. checked makes the overflow behavior explicit in the sample; the conversion is caught so the program can report the condition instead of terminating.
Round to the nearest integer
Convert.ToInt32(decimal) is not a truncating conversion. It rounds to the nearest 32-bit signed integer and uses midpoint-to-even rounding when the fractional part is exactly .5. In the verified sample, 12.5m becomes 12 because 12 is even, while 13.5m becomes 14. This is often appropriate when values are measurements that should be rounded to the nearest whole unit, but it is surprising if a caller expects every midpoint to move away from zero.
Choose Convert.ToInt32 only after confirming that midpoint-to-even is the policy your application needs. Its decimal overload documentation also specifies OverflowException for values outside the int range. It is not a way to make a too-large decimal safe.
Choose an explicit rounding rule
For directed rounding, call a Math method on the decimal first, then convert the already integral result. Math.Floor moves toward negative infinity, and Math.Ceiling moves toward positive infinity. These rules differ from truncation for negative fractions: Math.Floor(-12.9m) is -13m, whereas (int)-12.9m is -12.
When you need nearest rounding with a non-default tie rule, state it in code. For example, Math.Round(value, MidpointRounding.AwayFromZero) makes 2.5m become 3m and -2.5m become -3m. In contrast, the default Math.Round(decimal) uses midpoint-to-even. Math.Truncate is another readable option when you want the same toward-zero integral decimal before converting it to int.
Do not cast before rounding: (int)value has already removed the fraction, so a later call to Math.Round cannot restore the intended choice. Round first, verify the result is in range, then cast.
Handle values outside the int range
An int is a 32-bit signed integer, from int.MinValue through int.MaxValue. A decimal can represent values far outside that range. A direct conversion of an out-of-range value throws OverflowException; it does not clamp the value to the nearest endpoint.
If an out-of-range input is expected, validate it before converting or catch OverflowException at the boundary where the value enters your application. Do not silently cast a monetary value just to fit an API: choose a wider integer type or keep decimal when the receiving contract permits it. A checked conversion is useful when a narrow type is intentional and overflow must be visible during testing.
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 |
Negative values make these differ from truncation. |
| Custom midpoint behavior | Math.Round with MidpointRounding, then cast |
State the tie-breaking rule explicitly. |
For ordinary truncation, the explicit cast is the recommended and concise answer. Select Convert.ToInt32 or a Math method only when its rounding policy is the rule you mean to apply, and always account for an OverflowException at the int boundary.