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
Convert.ToInt32 Method in C#
The Convert.ToInt32() method in C# converts a specified value to a 32-bit signed integer. This method provides type conversion from various data types including string, double, float, decimal, and bool to int.
The method uses rounding to nearest even (banker's rounding) when converting floating-point numbers, and throws exceptions for invalid conversions like null strings or out-of-range values.
Syntax
Following are the common overloads of Convert.ToInt32() method −
Convert.ToInt32(object value) Convert.ToInt32(string value) Convert.ToInt32(double value) Convert.ToInt32(bool value)
Parameters
value − The value to convert to a 32-bit signed integer.
Return Value
Returns a 32-bit signed integer equivalent to the specified value, or zero if the value is null.
Converting from Double
Example
using System;
public class Demo {
public static void Main() {
double doubleNum = 11.53;
int intNum = Convert.ToInt32(doubleNum);
Console.WriteLine("Converted {0} to {1}", doubleNum, intNum);
// Banker's rounding examples
Console.WriteLine("2.5 converts to: " + Convert.ToInt32(2.5));
Console.WriteLine("3.5 converts to: " + Convert.ToInt32(3.5));
Console.WriteLine("-2.5 converts to: " + Convert.ToInt32(-2.5));
}
}
The output of the above code is −
Converted 11.53 to 12 2.5 converts to: 2 3.5 converts to: 4 -2.5 converts to: -2
Converting from String
Example
using System;
public class Demo {
public static void Main() {
string[] values = {"123", "-456", "0", "789"};
foreach(string value in values) {
try {
int result = Convert.ToInt32(value);
Console.WriteLine("'{0}' converts to {1}", value, result);
}
catch(FormatException) {
Console.WriteLine("'{0}' is not a valid integer format", value);
}
}
}
}
The output of the above code is −
'123' converts to 123 '-456' converts to -456 '0' converts to 0 '789' converts to 789
Converting from Boolean
Example
using System;
public class Demo {
public static void Main() {
bool trueValue = true;
bool falseValue = false;
int fromTrue = Convert.ToInt32(trueValue);
int fromFalse = Convert.ToInt32(falseValue);
Console.WriteLine("true converts to: " + fromTrue);
Console.WriteLine("false converts to: " + fromFalse);
}
}
The output of the above code is −
true converts to: 1 false converts to: 0
Exception Handling
The Convert.ToInt32() method throws exceptions for invalid inputs −
Example
using System;
public class Demo {
public static void Main() {
string[] invalidValues = {"abc", "", "2147483648"};
foreach(string value in invalidValues) {
try {
int result = Convert.ToInt32(value);
Console.WriteLine("'{0}' converts to {1}", value, result);
}
catch(FormatException) {
Console.WriteLine("'{0}' - FormatException: Invalid format", value);
}
catch(OverflowException) {
Console.WriteLine("'{0}' - OverflowException: Value too large", value);
}
}
// null converts to 0
Console.WriteLine("null converts to: " + Convert.ToInt32(null));
}
}
The output of the above code is −
'abc' - FormatException: Invalid format '' - FormatException: Invalid format '2147483648' - OverflowException: Value too large null converts to: 0
Conclusion
The Convert.ToInt32() method provides a robust way to convert various data types to integers with built-in error handling. It uses banker's rounding for floating-point numbers and returns zero for null values, making it ideal for safe type conversions in C# applications.
