Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Decimal.ToInt32() Method in C#
The Decimal.ToInt32() method in C# is used to convert the value of the specified Decimal to the equivalent 32-bit signed integer. This method performs truncation, meaning it removes the fractional part without rounding.
Syntax
Following is the syntax −
public static int ToInt32(decimal val);
Parameters
val − The decimal number to convert to a 32-bit signed integer.
Return Value
Returns an int value that represents the truncated decimal value. If the decimal has fractional digits, they are discarded (not rounded).
How It Works
The method truncates the decimal value toward zero, removing any fractional part. This means 3.99 becomes 3, and -3.99 becomes -3.
Using Decimal.ToInt32() with Positive Values
using System;
public class Demo {
public static void Main() {
decimal val1 = 0.001m;
decimal val2 = 1.000m;
decimal val3 = 15.789m;
Console.WriteLine("Decimal 1 = " + val1);
Console.WriteLine("Decimal 2 = " + val2);
Console.WriteLine("Decimal 3 = " + val3);
int res1 = Decimal.ToInt32(val1);
int res2 = Decimal.ToInt32(val2);
int res3 = Decimal.ToInt32(val3);
Console.WriteLine("32-bit signed integer (val1) = " + res1);
Console.WriteLine("32-bit signed integer (val2) = " + res2);
Console.WriteLine("32-bit signed integer (val3) = " + res3);
}
}
The output of the above code is −
Decimal 1 = 0.001 Decimal 2 = 1.000 Decimal 3 = 15.789 32-bit signed integer (val1) = 0 32-bit signed integer (val2) = 1 32-bit signed integer (val3) = 15
Using Decimal.ToInt32() with Negative Values
using System;
public class Demo {
public static void Main() {
decimal val1 = -3.578m;
decimal val2 = 9.352m;
decimal val3 = -0.999m;
Console.WriteLine("Decimal 1 = " + val1);
Console.WriteLine("Decimal 2 = " + val2);
Console.WriteLine("Decimal 3 = " + val3);
int res1 = Decimal.ToInt32(val1);
int res2 = Decimal.ToInt32(val2);
int res3 = Decimal.ToInt32(val3);
Console.WriteLine("32-bit signed integer (val1) = " + res1);
Console.WriteLine("32-bit signed integer (val2) = " + res2);
Console.WriteLine("32-bit signed integer (val3) = " + res3);
}
}
The output of the above code is −
Decimal 1 = -3.578 Decimal 2 = 9.352 Decimal 3 = -0.999 32-bit signed integer (val1) = -3 32-bit signed integer (val2) = 9 32-bit signed integer (val3) = 0
Exception Handling
The method throws an OverflowException if the decimal value is outside the range of a 32-bit signed integer −
using System;
public class Demo {
public static void Main() {
try {
decimal largeValue = 3000000000m; // Outside int range
int result = Decimal.ToInt32(largeValue);
Console.WriteLine("Result: " + result);
}
catch (OverflowException ex) {
Console.WriteLine("OverflowException: " + ex.Message);
}
// Valid range example
decimal validValue = 2147483647m; // Max int value
int validResult = Decimal.ToInt32(validValue);
Console.WriteLine("Valid conversion: " + validResult);
}
}
The output of the above code is −
OverflowException: Value was either too large or too small for an Int32. Valid conversion: 2147483647
Conclusion
The Decimal.ToInt32() method converts decimal values to 32-bit signed integers by truncating the fractional part. It handles both positive and negative values, but throws an OverflowException if the decimal value exceeds the integer range (-2,147,483,648 to 2,147,483,647).
