Convert.ToInt32 Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Converts a specified value to a 32-bit signed integer.
Overloads
| Name | Description |
|---|---|
| ToInt32(String) |
Converts the specified string representation of a number to an equivalent 32-bit signed integer. |
| ToInt32(UInt16) |
Converts the value of the specified 16-bit unsigned integer to the equivalent 32-bit signed integer. |
| ToInt32(UInt32) |
Converts the value of the specified 32-bit unsigned integer to an equivalent 32-bit signed integer. |
| ToInt32(Single) |
Converts the value of the specified single-precision floating-point number to an equivalent 32-bit signed integer. |
| ToInt32(Object, IFormatProvider) |
Converts the value of the specified object to a 32-bit signed integer, using the specified culture-specific formatting information. |
| ToInt32(String, IFormatProvider) |
Converts the specified string representation of a number to an equivalent 32-bit signed integer, using the specified culture-specific formatting information. |
| ToInt32(String, Int32) |
Converts the string representation of a number in a specified base to an equivalent 32-bit signed integer. |
| ToInt32(UInt64) |
Converts the value of the specified 64-bit unsigned integer to an equivalent 32-bit signed integer. |
| ToInt32(SByte) |
Converts the value of the specified 8-bit signed integer to the equivalent 32-bit signed integer. |
| ToInt32(Object) |
Converts the value of the specified object to a 32-bit signed integer. |
| ToInt32(Int64) |
Converts the value of the specified 64-bit signed integer to an equivalent 32-bit signed integer. |
| ToInt32(Byte) |
Converts the value of the specified 8-bit unsigned integer to the equivalent 32-bit signed integer. |
| ToInt32(Char) |
Converts the value of the specified Unicode character to the equivalent 32-bit signed integer. |
| ToInt32(DateTime) |
Calling this method always throws InvalidCastException. |
| ToInt32(Boolean) |
Converts the specified Boolean value to the equivalent 32-bit signed integer. |
| ToInt32(Double) |
Converts the value of the specified double-precision floating-point number to an equivalent 32-bit signed integer. |
| ToInt32(Int16) |
Converts the value of the specified 16-bit signed integer to an equivalent 32-bit signed integer. |
| ToInt32(Int32) |
Returns the specified 32-bit signed integer; no actual conversion is performed. |
| ToInt32(Decimal) |
Converts the value of the specified decimal number to an equivalent 32-bit signed integer. |
ToInt32(String)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Converts the specified string representation of a number to an equivalent 32-bit signed integer.
public:
static int ToInt32(System::String ^ value);
public static int ToInt32(string value);
public static int ToInt32(string? value);
static member ToInt32 : string -> int
Public Shared Function ToInt32 (value As String) As Integer
Parameters
- value
- String
A string that contains the number to convert.
Returns
A 32-bit signed integer that is equivalent to the number in value, or 0 (zero) if value is null.
Exceptions
value does not consist of an optional sign followed by a sequence of digits (0 through 9).
value represents a number that is less than Int32.MinValue or greater than Int32.MaxValue.
Examples
The following example attempts to convert each element in a numeric string array to an integer.
string[] values = { "One", "1.34e28", "-26.87", "-18", "-6.00",
" 0", "137", "1601.9", Int32.MaxValue.ToString() };
int result;
foreach (string value in values)
{
try {
result = Convert.ToInt32(value);
Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.",
value.GetType().Name, value, result.GetType().Name, result);
}
catch (OverflowException) {
Console.WriteLine("{0} is outside the range of the Int32 type.", value);
}
catch (FormatException) {
Console.WriteLine("The {0} value '{1}' is not in a recognizable format.",
value.GetType().Name, value);
}
}
// The example displays the following output:
// The String value 'One' is not in a recognizable format.
// The String value '1.34e28' is not in a recognizable format.
// The String value '-26.87' is not in a recognizable format.
// Converted the String value '-18' to the Int32 value -18.
// The String value '-6.00' is not in a recognizable format.
// Converted the String value ' 0' to the Int32 value 0.
// Converted the String value '137' to the Int32 value 137.
// The String value '1601.9' is not in a recognizable format.
// Converted the String value '2147483647' to the Int32 value 2147483647.
let values =
[| "One"; "1.34e28"; "-26.87"; "-18"; "-6.00"
" 0"; "137"; "1601.9"; string Int32.MaxValue |]
for value in values do
try
let result = Convert.ToInt32 value
printfn $"Converted the {value.GetType().Name} value {value} to the {result.GetType().Name} value {result}."
with
| :? OverflowException ->
printfn $"{value} is outside the range of the Int32 type."
| :? FormatException ->
printfn $"The {value.GetType().Name} value '{value}' is not in a recognizable format."
// The example displays the following output:
// The String value 'One' is not in a recognizable format.
// The String value '1.34e28' is not in a recognizable format.
// The String value '-26.87' is not in a recognizable format.
// Converted the String value '-18' to the Int32 value -18.
// The String value '-6.00' is not in a recognizable format.
// Converted the String value ' 0' to the Int32 value 0.
// Converted the String value '137' to the Int32 value 137.
// The String value '1601.9' is not in a recognizable format.
// Converted the String value '2147483647' to the Int32 value 2147483647.
Dim values() As String = { "One", "1.34e28", "-26.87", "-18", "-6.00", _
" 0", "137", "1601.9", Int32.MaxValue.ToString() }
Dim result As Integer
For Each value As String In values
Try
result = Convert.ToInt32(value)
Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", _
value.GetType().Name, value, result.GetType().Name, result)
Catch e As OverflowException
Console.WriteLine("{0} is outside the range of the Int32 type.", value)
Catch e As FormatException
Console.WriteLine("The {0} value '{1}' is not in a recognizable format.", _
value.GetType().Name, value)
End Try
Next
' The example displays the following output:
' The String value 'One' is not in a recognizable format.
' The String value '1.34e28' is not in a recognizable format.
' The String value '-26.87' is not in a recognizable format.
' Converted the String value '-18' to the Int32 value -18.
' The String value '-6.00' is not in a recognizable format.
' Converted the String value ' 0' to the Int32 value 0.
' Converted the String value '137' to the Int32 value 137.
' The String value '1601.9' is not in a recognizable format.
' Converted the String value '2147483647' to the Int32 value 2147483647.
Remarks
Using the ToInt32(String) method is equivalent to passing value to the Int32.Parse(String) method.value is interpreted by using the formatting conventions of the current culture.
If you prefer not to handle an exception if the conversion fails, you can call the Int32.TryParse method instead. It returns a Boolean value that indicates whether the conversion succeeded or failed.
Applies to
ToInt32(UInt16)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Important
This API is not CLS-compliant.
Converts the value of the specified 16-bit unsigned integer to the equivalent 32-bit signed integer.
public:
static int ToInt32(System::UInt16 value);
[System.CLSCompliant(false)]
public static int ToInt32(ushort value);
[<System.CLSCompliant(false)>]
static member ToInt32 : uint16 -> int
Public Shared Function ToInt32 (value As UShort) As Integer
Parameters
- value
- UInt16
The 16-bit unsigned integer to convert.
Returns
A 32-bit signed integer that is equivalent to value.
- Attributes
Examples
The following example converts each element in an array of 16-bit unsigned integers to an integer.
ushort[] numbers = { UInt16.MinValue, 121, 340, UInt16.MaxValue };
int result;
foreach (ushort number in numbers)
{
try {
result = Convert.ToInt32(number);
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
number.GetType().Name, number,
result.GetType().Name, result);
}
catch (OverflowException) {
Console.WriteLine("The {0} value {1} is outside the range of the Int32 type.",
number.GetType().Name, number);
}
}
// The example displays the following output:
// Converted the UInt16 value 0 to the Int32 value 0.
// Converted the UInt16 value 121 to the Int32 value 121.
// Converted the UInt16 value 340 to the Int32 value 340.
// Converted the UInt16 value 65535 to the Int32 value 65535.
let numbers =
[| UInt16.MinValue; 121us; 340us; UInt16.MaxValue |]
for number in numbers do
try
let result = Convert.ToInt32 number
printfn $"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."
with :? OverflowException ->
printfn $"The {number.GetType().Name} value {number} is outside the range of the Int32 type."
// The example displays the following output:
// Converted the UInt16 value 0 to the Int32 value 0.
// Converted the UInt16 value 121 to the Int32 value 121.
// Converted the UInt16 value 340 to the Int32 value 340.
// Converted the UInt16 value 65535 to the Int32 value 65535.
Dim numbers() As UShort = { UInt16.MinValue, 121, 340, UInt16.MaxValue }
Dim result As Integer
For Each number As UShort In numbers
Try
result = Convert.ToInt32(number)
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", _
number.GetType().Name, number, _
result.GetType().Name, result)
Catch e As OverflowException
Console.WriteLine("The {0} value {1} is outside the range of the Int32 type.", _
number.GetType().Name, number)
End Try
Next
' The example displays the following output:
' Converted the UInt16 value 0 to the Int32 value 0.
' Converted the UInt16 value 121 to the Int32 value 121.
' Converted the UInt16 value 340 to the Int32 value 340.
' Converted the UInt16 value 65535 to the Int32 value 65535.
Applies to
ToInt32(UInt32)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Important
This API is not CLS-compliant.
Converts the value of the specified 32-bit unsigned integer to an equivalent 32-bit signed integer.
public:
static int ToInt32(System::UInt32 value);
[System.CLSCompliant(false)]
public static int ToInt32(uint value);
[<System.CLSCompliant(false)>]
static member ToInt32 : uint32 -> int
Public Shared Function ToInt32 (value As UInteger) As Integer
Parameters
- value
- UInt32
The 32-bit unsigned integer to convert.
Returns
A 32-bit signed integer that is equivalent to value.
- Attributes
Exceptions
value is greater than Int32.MaxValue.
Examples
The following example attempts to convert each element in an array of unsigned integers to a signed integer.
uint[] numbers = { UInt32.MinValue, 121, 340, UInt32.MaxValue };
int result;
foreach (uint number in numbers)
{
try {
result = Convert.ToInt32(number);
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
number.GetType().Name, number,
result.GetType().Name, result);
}
catch (OverflowException) {
Console.WriteLine("The {0} value {1} is outside the range of the Int32 type.",
number.GetType().Name, number);
}
}
// The example displays the following output:
// Converted the UInt32 value 0 to the Int32 value 0.
// Converted the UInt32 value 121 to the Int32 value 121.
// Converted the UInt32 value 340 to the Int32 value 340.
// The UInt32 value 4294967295 is outside the range of the Int32 type.
let numbers =
[| UInt32.MinValue; 121u; 340u; UInt32.MaxValue |]
for number in numbers do
try
let result = Convert.ToInt32 number
printfn $"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."
with :? OverflowException ->
printfn $"The {number.GetType().Name} value {number} is outside the range of the Int32 type."
// The example displays the following output:
// Converted the UInt32 value 0 to the Int32 value 0.
// Converted the UInt32 value 121 to the Int32 value 121.
// Converted the UInt32 value 340 to the Int32 value 340.
// The UInt32 value 4294967295 is outside the range of the Int32 type.
Dim numbers() As UInteger = { UInt32.MinValue, 121, 340, UInt32.MaxValue }
Dim result As Integer
For Each number As UInteger In numbers
Try
result = Convert.ToInt32(number)
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", _
number.GetType().Name, number, _
result.GetType().Name, result)
Catch e As OverflowException
Console.WriteLine("The {0} value {1} is outside the range of the Int32 type.", _
number.GetType().Name, number)
End Try
Next
' The example displays the following output:
' Converted the UInt32 value 0 to the Int32 value 0.
' Converted the UInt32 value 121 to the Int32 value 121.
' Converted the UInt32 value 340 to the Int32 value 340.
' The UInt32 value 4294967295 is outside the range of the Int32 type.
Applies to
ToInt32(Single)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Converts the value of the specified single-precision floating-point number to an equivalent 32-bit signed integer.
public:
static int ToInt32(float value);
public static int ToInt32(float value);
static member ToInt32 : single -> int
Public Shared Function ToInt32 (value As Single) As Integer
Parameters
- value
- Single
The single-precision floating-point number to convert.
Returns
value, rounded to the nearest 32-bit signed integer. If value is halfway between two whole numbers, the even number is returned; that is, 4.5 is converted to 4, and 5.5 is converted to 6.
Exceptions
value is greater than Int32.MaxValue or less than Int32.MinValue.
Examples
The following example attempts to convert each element in an array of Single values to an integer.
float[] values= { Single.MinValue, -1.38e10f, -1023.299f, -12.98f,
0f, 9.113e-16f, 103.919f, 17834.191f, Single.MaxValue };
int result;
foreach (float value in values)
{
try {
result = Convert.ToInt32(value);
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
value.GetType().Name, value, result.GetType().Name, result);
}
catch (OverflowException) {
Console.WriteLine("{0} is outside the range of the Int32 type.", value);
}
}
// The example displays the following output:
// -3.40282346638529E+38 is outside the range of the Int32 type.
// -13799999488 is outside the range of the Int32 type.
// Converted the Double value -1023.29901123047 to the Int32 value -1023.
// Converted the Double value -12.9799995422363 to the Int32 value -13.
// Converted the Double value 0 to the Int32 value 0.
// Converted the Double value 9.11299983940444E-16 to the Int32 value 0.
// Converted the Double value 103.918998718262 to the Int32 value 104.
// Converted the Double value 17834.19140625 to the Int32 value 17834.
// 3.40282346638529E+38 is outside the range of the Int32 type.
let values =
[| Single.MinValue; -1.38e10f; -1023.299f; -12.98f
0f; 9.113e-16f; 103.919f; 17834.191f; Single.MaxValue |]
for value in values do
try
let result = Convert.ToInt32 value
printfn $"Converted the {value.GetType().Name} value {value} to the {result.GetType().Name} value {result}."
with :? OverflowException ->
printfn $"{value} is outside the range of the Int32 type."
// The example displays the following output:
// -3.40282346638529E+38 is outside the range of the Int32 type.
// -13799999488 is outside the range of the Int32 type.
// Converted the Double value -1023.29901123047 to the Int32 value -1023.
// Converted the Double value -12.9799995422363 to the Int32 value -13.
// Converted the Double value 0 to the Int32 value 0.
// Converted the Double value 9.11299983940444E-16 to the Int32 value 0.
// Converted the Double value 103.918998718262 to the Int32 value 104.
// Converted the Double value 17834.19140625 to the Int32 value 17834.
// 3.40282346638529E+38 is outside the range of the Int32 type.
Dim values() As Single = { Single.MinValue, -1.38e10, -1023.299, -12.98, _
0, 9.113e-16, 103.919, 17834.191, Single.MaxValue }
Dim result As Integer
For Each value As Single In values
Try
result = Convert.ToInt32(value)
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", _
value.GetType().Name, value, result.GetType().Name, result)
Catch e As OverflowException
Console.WriteLine("{0} is outside the range of the Int32 type.", value)
End Try
Next
' The example displays the following output:
' -3.40282346638529E+38 is outside the range of the Int32 type.
' -13799999488 is outside the range of the Int32 type.
' Converted the Double value -1023.29901123047 to the Int32 value -1023.
' Converted the Double value -12.9799995422363 to the Int32 value -13.
' Converted the Double value 0 to the Int32 value 0.
' Converted the Double value 9.11299983940444E-16 to the Int32 value 0.
' Converted the Double value 103.918998718262 to the Int32 value 104.
' Converted the Double value 17834.19140625 to the Int32 value 17834.
' 3.40282346638529E+38 is outside the range of the Int32 type.
See also
Applies to
ToInt32(Object, IFormatProvider)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Converts the value of the specified object to a 32-bit signed integer, using the specified culture-specific formatting information.
public:
static int ToInt32(System::Object ^ value, IFormatProvider ^ provider);
public static int ToInt32(object value, IFormatProvider provider);
public static int ToInt32(object? value, IFormatProvider? provider);
static member ToInt32 : obj * IFormatProvider -> int
Public Shared Function ToInt32 (value As Object, provider As IFormatProvider) As Integer