C# में Decimal.ToInt16() मेथड का उपयोग निर्दिष्ट Decimal के मान को समतुल्य 16-बिट साइन्ड पूर्णांक में बदलने के लिए किया जाता है।
सिंटैक्स
निम्नलिखित सिंटैक्स है:
public static short ToInt16 (decimal val);
यहाँ, val परिवर्तित करने के लिए दशमलव संख्या है।
उदाहरण
आइए अब Decimal.ToInt16() मेथड को लागू करने का एक उदाहरण देखें:
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);
}
}
आउटपुट
यह निम्नलिखित आउटपुट उत्पन्न करेगा:
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
उदाहरण
आइए अब Decimal.ToInt16() मेथड को लागू करने का एक और उदाहरण देखें:
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);
}
}
आउटपुट
यह निम्नलिखित आउटपुट उत्पन्न करेगा:
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