此方法用於將指定的Decimal的值轉換為等效的單精度浮點數。此方法可能會產生舍入錯誤,因為單精度浮點數的有效位數比十進製數少。
用法: public static float ToSingle (decimal d);
Here, d is the decimal number which is to be converted.
返回值:它返回等於d的單精度浮點數。
例:
// C# program to demonstrate the
// Decimal.ToSingle(Decimal) Method
using System;
class GFG {
// Main Method
public static void Main()
{
try {
// Taking decimal variables
Decimal dec1 = 0.0000000000134563456789M;
Decimal dec2 = 4589662514452860951234M;
// using ToSingle(Decimal) Method
float val1 = Decimal.ToSingle(dec1);
// using ToSingle(Decimal) Method
float val2 = Decimal.ToSingle(dec2);
// Printing the float value
Console.WriteLine("The float value "
+ "is : {0}", val1);
// Printing the float value
Console.WriteLine("The float value "
+ "is : {0}", val2);
}
catch (OverflowException e)
{
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
輸出:
The float value is : 1.345635E-11 The float value is : 4.589663E+21
參考:
相關用法
- C# Dictionary.Add()用法及代碼示例
- C# Math.Abs()方法用法及代碼示例
- C# Math.Abs()函數用法及代碼示例
- C# Math.Exp()用法及代碼示例
- C# Queue.Contains()用法及代碼示例
- C# Stack.Pop()用法及代碼示例
注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 Decimal.ToSingle() Method in C#。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。