What is Decimal.ToInt16() in C#?

Overview

Decimal.ToInt16() is a C# method to convert a decimal number to its equivalent of a 16-bit signed integer.

Syntax

public static short ToInt16 (decimal d);

Parameters

d: This is the decimal value we want to convert.

Return value

The value returned is the 16-bit integer equivalent of the decimal number d.

Code example

// use System
using System;
// create class
class DecimalToInt16
{
// main method
static void Main()
{
// create decimal numbers
decimal d1 = 123.343m;
decimal d2 = -323.888m;
decimal d3 = 0.9999m;
// convert to int16
// and print results
Console.WriteLine(Decimal.ToInt16(d1));
Console.WriteLine(Decimal.ToInt16(d2));
Console.WriteLine(Decimal.ToInt16(d3));
}
}

Explanation

  • Lines 10-12: We create some decimal values d1, d2 and d3.

  • Lines 16-19: We call the Decimal.ToInt16() method on the decimal values we created and print the results to the console.

Free Resources