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 Systemusing System;// create classclass DecimalToInt16{// main methodstatic void Main(){// create decimal numbersdecimal d1 = 123.343m;decimal d2 = -323.888m;decimal d3 = 0.9999m;// convert to int16// and print resultsConsole.WriteLine(Decimal.ToInt16(d1));Console.WriteLine(Decimal.ToInt16(d2));Console.WriteLine(Decimal.ToInt16(d3));}}
Explanation
-
Lines 10-12: We create some decimal values
d1,d2andd3. -
Lines 16-19: We call the
Decimal.ToInt16()method on the decimal values we created and print the results to the console.