Phương thức Decimal.ToSingle() trong C# chuyển đổi giá trị Decimal được chỉ định thành số thực độ chính xác đơn (single-precision floating-point) tương đương.
Cú pháp
public static float ToSingle(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 = 0.00000007575833m;
Console.WriteLine("Decimal value = " + val);
float res = Decimal.ToSingle(val);
Console.WriteLine("Single-precision floating-point number = " + res);
}
}
Đầu ra
Decimal value = 0.00000007575833 Single-precision floating-point number = 7.575833E-08
Ví dụ 2
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);
}
}
Đầu ra
Decimal value = 8767576576 Single-precision floating-point number = 8.767576E+09