Decimal.ToInt16() Method in C#


The Decimal.ToInt16() method in C# is used to convert the value of the specified Decimal to the equivalent 16-bit signed integer.

Syntax

Following is the syntax −

public static short ToInt16 (decimal val);

Above, Val is the decimal number to convert.

Example

Let us now see an example to implement the Decimal.ToInt16() 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);
      short res1 = Decimal.ToInt16(val1);
      short res2 = Decimal.ToInt16(val2);
      Console.WriteLine("16-bit signed integer (value1) (Decimal to signed integer) = "+res1);
      Console.WriteLine("16-bit signed integer (value1) (Decimal to signed integer) = "+res2);
   }
}

Output

This will produce the following output −

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

Example

Let us now see another example to implement the Decimal.ToInt16() 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);
      short res1 = Decimal.ToInt16(val1);
      short res2 = Decimal.ToInt16(val2);
      Console.WriteLine("16-bit signed integer (value1) (Decimal to signed integer) = "+res1);
      Console.WriteLine("16-bit signed integer (value1) (Decimal to signed integer) = "+res2);
   }
}

Output

This will produce the following output −

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

Updated on: 12-Nov-2019

40 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements