C# 中的 Decimal.ToInt64() 方法


C# 中的 Decimal.ToInt64() 方法用于将指定 Decimal 的值转换为等效的 64 位有符号整数。

语法

以下是语法 -

public static long ToInt64 (decimal val);

上面,Val 是要转换的十进制数。

示例

现在我们看一个实现 Decimal.ToInt64() 方法的示例 -

using System;
public class Demo {
   public static void Main(){
      Decimal val1 = -567.7989m;
      Decimal val2 = 456.000m;
      Console.WriteLine("Decimal 1 = "+val1);
      Console.WriteLine("Decimal 2 = "+val2);
      long res1 = Decimal.ToInt64(val1);
      long res2 = Decimal.ToInt64(val2);
      Console.WriteLine("64-bit signed integer (value1) (Decimal to signed integer) = "+res1);
      Console.WriteLine("64-bit signed integer (value1) (Decimal to signed integer) = "+res2);
   }
}

输出

该方法将生成以下输出 -

Decimal 1 = -567.7989
Decimal 2 = 456.000
64-bit signed integer (value1) (Decimal to signed integer) = -567
64-bit signed integer (value1) (Decimal to signed integer) = 456

示例

现在我们看另一个实现 Decimal.ToInt64() 方法的示例 -

using System;
public class Demo {
   public static void Main(){
      Decimal val1 = 1.00m;
      Decimal val2 = 34.678m;
      Console.WriteLine("Decimal 1 = "+val1);
      Console.WriteLine("Decimal 2 = "+val2);
      long res1 = Decimal.ToInt64(val1);
      long res2 = Decimal.ToInt64(val2);
      Console.WriteLine("64-bit signed integer (value1) (Decimal to signed integer) = "+res1);
      Console.WriteLine("64-bit signed integer (value1) (Decimal to signed integer) = "+res2);
   }
}

输出

该方法将生成以下输出 -

Decimal 1 = 1.00
Decimal 2 = 34.678
64-bit signed integer (value1) (Decimal to signed integer) = 1
64-bit signed integer (value1) (Decimal to signed integer) = 34

更新时间:2019-11-12

90 个浏览

开启你的 职业生涯

通过完成本课程获得证书

开始
广告