วิธี Decimal.ToSingle() ใน C # ใช้เพื่อแปลงค่าของ Decimal ที่ระบุให้เป็นตัวเลขทศนิยมความแม่นยำเดียวที่เทียบเท่า
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
public static float ToSingle (decimal val);
ด้านบน Val คือตัวเลขทศนิยมที่จะแปลง
ตัวอย่าง
ให้เราดูตัวอย่างการใช้วิธีการ Decimal.ToSingle() -
using System;
public class Demo {
public static void Main(){
Decimal val = 0.00000007575833m;
Console.WriteLine("Decimal value = "+val);
float res = Decimal.ToSingle(val);
Console.WriteLine("Single-precision floating-point number = "+res);
}
} ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
Decimal value = 0.00000007575833 Single-precision floating-point number = 7.575833E-08
ตัวอย่าง
ให้เราดูตัวอย่างอื่นเพื่อใช้วิธีการ Decimal.ToSingle() -
using System;
public class Demo {
public static void Main(){
Decimal val = 8767576576m;
Console.WriteLine("Decimal value = "+val);
float res = Decimal.ToSingle(val);
Console.WriteLine("Single-precision floating-point number = "+res);
}
} ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
Decimal value = 8767576576 Single-precision floating-point number = 8.767576E+09