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.

Syntax

Following is the syntax −

public static int ToInt32 (decimal val);

Above, Val is the decimal number to convert.

Example

Let us now see an example to implement the Decimal.ToInt32() method −

using System;
public class Demo {
   public static void Main(){
      Decimal val1 = 0.001m;
      Decimal val2 = 1.000m;
      Console.WriteLine("Decimal 1 = "+val1);
      Console.WriteLine("Decimal 2 = "+val2);
      int res1 = Decimal.ToInt32(val1);
      int res2 = Decimal.ToInt32(val2);
      Console.WriteLine("32-bit signed integer (value1) (Decimal to signed integer) = "+res1);
      Console.WriteLine("32-bit signed integer (value1) (Decimal to signed integer) = "+res2);
   }
}

Output

This will produce the following output −

Decimal 1 = 0.001
Decimal 2 = 1.000
32-bit signed integer (value1) (Decimal to signed integer) = 0
32-bit signed integer (value1) (Decimal to signed integer) = 1

Example

Let us now see another example to implement the Decimal.ToInt32() method −

using System;
public class Demo {
   public static void Main(){
      Decimal val1 = -3.578m;
      Decimal val2 = 9.352m;
      Console.WriteLine("Decimal 1 = "+val1);
      Console.WriteLine("Decimal 2 = "+val2);
      int res1 = Decimal.ToInt32(val1);
      int res2 = Decimal.ToInt32(val2);
      Console.WriteLine("32-bit signed integer (value1) (Decimal to signed integer) = "+res1);
      Console.WriteLine("32-bit signed integer (value1) (Decimal to signed integer) = "+res2);
   }
}

Output

This will produce the following output −

Decimal 1 = -3.578
Decimal 2 = 9.352
32-bit signed integer (value1) (Decimal to signed integer) = -3
32-bit signed integer (value1) (Decimal to signed integer) = 9

Updated on: 12-Nov-2019

43 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements