Devdit
 

error CS0664: Literal of type double cannot be implicitly converted to type 'decimal' คือ วิธีแก้ไข

1.4K

เขียนภาษา C# ประกาศตัวแปรชนิด decimal สำหรับเก็บค่าตัวเลขทศนิยมการเงิน แต่รันแล้วขึ้น error CS0664: Literal of type double cannot be implicitly converted to type 'decimal' แบบนี้ต้องแก้ไขอย่างไร

using System;

class Program
{
    static void Main(string[] args)
    {
        decimal value = 50.69;
    }
}

 

วิธีแก้ไข

กรณีประกาศตัวแปรชนิด decimal ภาษา C# ต้องใส่ตัวอักษร M หรือ m หลังค่าข้อมูลชนิด decimal ด้วย ตัวอย่าง 50.69M หรือ 50.69m เช่นเดียวกับข้อมูลชนิด Double ควรใส่ได้ D หรือ d หลังค่าข้อมูล

using System;

class Program
{
    static void Main(string[] args)
    {
        decimal value = 50.69M;
    }
}
แก้ไข 2 ปีที่แล้ว
ชอบ
ลิ้งก์
แชร์
Devdit มีช่อง YouTube แล้ว
เราสร้างวิดีโอเกี่ยวกับเทคโนโลยี ทำตามง่ายๆ