Decimal.ToInt16()
is a C# method to convert a decimal number to its equivalent of a 16-bit signed integer.
public static short ToInt16 (decimal d);
d
: This is the decimal value we want to convert.
The value returned is the 16-bit integer equivalent of the decimal number d
.
// 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));}}
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.