The Decimal.ToInt32()
method in C# converts the value of the specified decimal to the equivalent 32-bit signed integer.
Note: If the whole number part of the decimal value is greater than a 32-bit signed integer, an error will be thrown upon conversion.
Int32
values range from -2,147,483,648
to
2,147,483,647
.
public static int ToInt32 (decimal d);
d
: This is the decimal value that we want to convert to the equivalent 32-bit signed integer.
An Int32
value is returned. It is a 32-bit signed integer equivalent of the specified decimal value.
// use Systemusing System;// create classclass DecimalToInt32{// main methodstatic void Main(){// create decimal valuesdecimal d1 = 323.45m;decimal d2 = 1.88455435m;decimal d3 = -0.34m;decimal d4 = 12.88888m;// convert to Int32// and print resultsConsole.WriteLine(Decimal.ToInt32(d1));Console.WriteLine(Decimal.ToInt32(d2));Console.WriteLine(Decimal.ToInt32(d3));Console.WriteLine(Decimal.ToInt32(d4));}}
Lines 11-14: We create variables d1
, d2
, d3
, and d4
of type decimal and assign decimal values to them.
Lines 18-21: We convert each value to a signed 32-bit integer using the Decimal.ToInt32()
method and print the results.