Decimal.ToDouble() 方法在 C# 中
C# 中的 Decimal.ToDouble() 方法用于将指定 Decimal 的值转换为等效的双精度浮点数。
语法
以下是语法 −
public static double ToDouble (decimal val);
上面,Val 是要转换的小数。
示例
下面我们来看一个示例,来说明如何实现 Decimal.ToDouble() 方法−
using System; public class Demo { public static void Main(){ Decimal val1 = -0.22m; Decimal val2 = -0.01m; Console.WriteLine("Decimal 1 = "+val1); Console.WriteLine("Decimal 2 = "+val2); Double res1 = Decimal.ToDouble(val1); Double res2 = Decimal.ToDouble(val2); Console.WriteLine("Double value1 (Decimal to Double) = "+res1); Console.WriteLine("Double value2 (Decimal to Double) = "+res2); } }
输出
将产生以下输出 −
Decimal 1 = -0.22 Decimal 2 = -0.01 Double value1 (Decimal to Double) = -0.22 Double value2 (Decimal to Double) = -0.01
示例
下面我们来看另一个示例,来说明如何实现 Decimal.ToDouble() 方法−
using System; public class Demo { public static void Main(){ Decimal val1 = 23828m; Decimal val2 = 976587687595766665m; Console.WriteLine("Decimal 1 = "+val1); Console.WriteLine("Decimal 2 = "+val2); Double res1 = Decimal.ToDouble(val1); Double res2 = Decimal.ToDouble(val2); Console.WriteLine("Double value1 (Decimal to Double) = "+res1); Console.WriteLine("Double value2 (Decimal to Double) = "+res2); } }
输出
将产生以下输出 −
Decimal 1 = 23828 Decimal 2 = 976587687595766665 Double value1 (Decimal to Double) = 23828 Double value2 (Decimal to Double) = 9.76587687595767E+17
广告