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

การแปลงโดยนัย (Implicit Conversion) จาก UInt32 เป็น Decimal ใน C#


ภาพรวม

การแปลงโดยนัย (implicit conversion) จาก uint (32-bit unsigned integer) เป็น decimal เป็นไปได้โดยตรง เพราะ decimal มีขอบเขตและความแม่นยำครอบคลุม uint หมด

รูปแบบการใช้งาน

uint val = 342741539;
decimal dec = val; // implicit conversion - ไม่ต้อง cast

ตัวอย่างโค้ดเต็ม

using System;

public class Demo
{
    public static void Main()
    {
        uint val = 342741539;
        decimal dec = val; // implicit
        Console.WriteLine("Decimal = " + dec);
    }
}

ผลลัพธ์

Decimal = 342741539

ตารางการแปลงโดยนัยของชนิดตัวเลข

จากไป (Implicit)
byteshort, ushort, int, uint, long, ulong, float, double, decimal
shortint, long, float, double, decimal
intlong, float, double, decimal
longfloat, double, decimal
uintulong, float, double, decimal
ulongfloat, double, decimal
floatdouble

หมายเหตุ: การแปลงเป็น float/double อาจสูญเสียความแม่นยำ แต่ไม่ล้น