Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

การแปลงนัยโดยนัยจาก Int16 เป็น Decimal ใน C#

ประเภท short แทนจำนวนเต็ม 16 บิต ที่มีเครื่องหมาย คือ Int16

เพื่อแปลงนัยโดยนัยจำนวนเต็ม 16 บิต มีเครื่องหมาย เป็น Decimal ขั้นแรกให้กำหนดค่า short:

short val = -32768;

เพื่อแปลง short เป็น decimal ให้กำหนดค่า:

dec = val;

มาดูตัวอย่างอีกตัวหนึ่ง

ตัวอย่าง

using System;

public class Demo
{
    public static void Main()
    {
        short val = -32768;
        decimal dec;
        Console.WriteLine("Implicit conversion from Int16 to Decimal");
        dec = val;
        Console.WriteLine("Decimal : " + dec);
    }
}

ผลลัพธ์

Implicit conversion from Int16 to Decimal
Decimal : -32768