• Register
3,890 points
11 5 1
Welcome to Kodlogs, programming questions and answer website.

Decimal.ToInt32() method is used to convert the specified decimal values into 32 bit signed integer value. The users can also convert a Decimal to 32 bits integer by using the Explicit assignment operator.

Syntax:

public static int ToInt32(decimal value); // here the value of the decimal number is required to convert.

Return Value:

It will return 32 bit signed integer into a specified value.

Exception:

This will give OverflowException if the specified value is less than the main value or greater than the max value.

Example Code:

// C# program to demonstrate the

// Decimal.ToInt32(Decimal) Method

using System;

class GFG {

 // Main Method

public static void Main()

               {

                    try {
                // Taking decimal variables

                     Decimal dec1 = 2147483647M;

                     Decimal dec2 = 21458565.2996m;

                             // using Decimal.ToInt32(Decimal) Method

                             // Here int means Int32

                             int val1 = Decimal.ToInt32(dec1);  

                              // using Decimal.ToInt32(Decimal) Method

                             // Here int means Int32

                             int val2 = Decimal.ToInt32(dec2);

                            // Printing the Int32 value

                             Console.WriteLine("The Int32 value " + "is : {0}", val1);

                              // Printing the Int32 value

                              Console.WriteLine("The Int32 value " + "is : {0}", val2);

                           }

                               catch (OverflowException e)

                               {

                          Console.Write("Exception Thrown: ");

                        Console.Write("{0}", e.GetType(), e.Message);

                               }

               }

}

 

Output:

The Int32 value is: 2147483647

The Int32 value is: 21458565

 

3,890 points
11 5 1