Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Phương thức Decimal.ToUInt16() trong C#

Phương thức Decimal.ToUInt16() trong C# chuyển đổi giá trị Decimal được chỉ định thành số nguyên không dấu 16-bit tương đương.

Cú pháp

public static ushort ToUInt16(decimal val);

Tham số val là số decimal cần chuyển đổi.

Ví dụ 1

using System;
public class Demo
{
    public static void Main()
    {
        Decimal val = 875.647m;
        Console.WriteLine("Decimal value = " + val);
        ushort res = Decimal.ToUInt16(val);
        Console.WriteLine("16-bit unsigned integer = " + res);
    }
}

Đầu ra

Decimal value = 875.647
16-bit unsigned integer = 875

Ví dụ 2

using System;
public class Demo
{
    public static void Main()
    {
        Decimal val = 0.001m;
        Console.WriteLine("Decimal value = " + val);
        ushort res = Decimal.ToUInt16(val);
        Console.WriteLine("16-bit unsigned integer = " + res);
    }
}

Đầu ra

Decimal value = 0.001
16-bit unsigned integer = 0