信息
您所需的主题如下所示。但此主题未包含在此库中。

Decimal.ToInt64 方法

2013/12/13

将指定的 Decimal 值转换为等效的 64 位有符号整数。

Namespace:  System
程序集:  mscorlib(位于 mscorlib.dll 中)

public static long ToInt64(
	decimal d
)

参数

d
类型: System.Decimal
要转换的 Decimal 值。

返回值

类型: System.Int64
d 的值等效的 64 位带符号整数。

异常条件
OverflowException

d 小于 Int64.MinValue 或大于 Int64.MaxValue

返回值是十进制值的整数部分;小数位被截断。

下面的代码示例使用 ToInt64 方法,将 Decimal 数字转换为 Int64 值。


// Example of the decimal.ToInt64 and decimal.ToUInt64 methods.
using System;

class Example
{
   const string formatter = "{0,25}{1,22}{2,22}";

   // Get the exception type name; remove the namespace prefix.
   public static string GetExceptionType(Exception ex)
   {
      string exceptionType = ex.GetType().ToString();
      return exceptionType.Substring(
          exceptionType.LastIndexOf('.') + 1);
   }

   // Convert the decimal argument; catch exceptions that are thrown.
   public static void DecimalToU_Int64(System.Windows.Controls.TextBlock outputBlock, decimal argument)
   {
      object Int64Value;
      object UInt64Value;

      // Convert the argument to a long value.
      try
      {
         Int64Value = decimal.ToInt64(argument);
      }
      catch (Exception ex)
      {
         Int64Value = GetExceptionType(ex);
      }

      // Convert the argument to a ulong value.
      try
      {
         UInt64Value = decimal.ToUInt64(argument);
      }
      catch (Exception ex)
      {
         UInt64Value = GetExceptionType(ex);
      }

      outputBlock.Text += String.Format(formatter, argument,
          Int64Value, UInt64Value) + "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += String.Format("This example of the \n" +
          "  decimal.ToInt64( decimal ) and \n" +
          "  decimal.ToUInt64( decimal ) \nmethods " +
          "generates the following output. It \ndisplays " +
          "several converted decimal values.\n") + "\n";
      outputBlock.Text += String.Format(formatter, "decimal argument",
          "long/exception", "ulong/exception") + "\n";
      outputBlock.Text += String.Format(formatter, "----------------",
          "--------------", "---------------") + "\n";

      // Convert decimal values and display the results.
      DecimalToU_Int64(outputBlock, 123M);
      DecimalToU_Int64(outputBlock, new decimal(123000, 0, 0, false, 3));
      DecimalToU_Int64(outputBlock, 123.999M);
      DecimalToU_Int64(outputBlock, 18446744073709551615.999M);
      DecimalToU_Int64(outputBlock, 18446744073709551616M);
      DecimalToU_Int64(outputBlock, 9223372036854775807.999M);
      DecimalToU_Int64(outputBlock, 9223372036854775808M);
      DecimalToU_Int64(outputBlock, -0.999M);
      DecimalToU_Int64(outputBlock, -1M);
      DecimalToU_Int64(outputBlock, -9223372036854775808.999M);
      DecimalToU_Int64(outputBlock, -9223372036854775809M);
   }
}

/*
This example of the
  decimal.ToInt64( decimal ) and
  decimal.ToUInt64( decimal )
methods generates the following output. It
displays several converted decimal values.

         decimal argument        long/exception       ulong/exception
         ----------------        --------------       ---------------
                      123                   123                   123
                  123.000                   123                   123
                  123.999                   123                   123
 18446744073709551615.999     OverflowException  18446744073709551615
     18446744073709551616     OverflowException     OverflowException
  9223372036854775807.999   9223372036854775807   9223372036854775807
      9223372036854775808     OverflowException   9223372036854775808
                   -0.999                     0                     0
                       -1                    -1     OverflowException
 -9223372036854775808.999  -9223372036854775808     OverflowException
     -9223372036854775809     OverflowException     OverflowException
*/


Windows Phone OS

受以下版本支持: 8.0, 7.1, 7.0

Windows Phone

显示: