Freigeben über


Decimal.ToUInt64 Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Converts the value of the specified Decimal to the equivalent 64-bit unsigned integer.

This API is not CLS-compliant. 

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
<CLSCompliantAttribute(False)> _
Public Shared Function ToUInt64 ( _
    d As Decimal _
) As ULong
[SecuritySafeCriticalAttribute]
[CLSCompliantAttribute(false)]
public static ulong ToUInt64(
    decimal d
)

Parameters

Return Value

Type: System.UInt64
A 64-bit unsigned integer equivalent to the value of d.

Exceptions

Exception Condition
OverflowException

d is negative or greater than UInt64.MaxValue.

Remarks

The return value is the integral part of the decimal value; fractional digits are truncated.

Examples

The following code example converts Decimal numbers to UInt64 values using the ToUInt64 method.

' Example of the Decimal.ToInt64 and Decimal.ToUInt64 methods.

Module Example

   Dim formatter As String = "{0,25}{1,22}{2,22}"

   ' Get the exception type name; remove the namespace prefix.
   Function GetExceptionType(ByVal ex As Exception) As String

      Dim exceptionType As String = ex.GetType().ToString()
      Return exceptionType.Substring( _
          exceptionType.LastIndexOf("."c) + 1)
   End Function

   ' Convert the Decimal argument; catch exceptions that are thrown.
   Sub DecimalToU_Int64(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal argument As Decimal)

      Dim Int64Value As Object
      Dim UInt64Value As Object

      ' Convert the argument to a Long value.
      Try
         Int64Value = Decimal.ToInt64(argument)
      Catch ex As Exception
         Int64Value = GetExceptionType(ex)
      End Try

      ' Convert the argument to a UInt64 value.
      Try
         UInt64Value = Decimal.ToUInt64(argument)
      Catch ex As Exception
         UInt64Value = GetExceptionType(ex)
      End Try

      outputBlock.Text &= String.Format(formatter, argument, _
          Int64Value, UInt64Value) & vbCrLf
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      outputBlock.Text &= String.Format("This example of the " & vbCrLf & _
          "  Decimal.ToInt64( Decimal ) and " & vbCrLf & _
          "  Decimal.ToUInt64( Decimal ) " & vbCrLf & "methods " & _
          "generates the following output. It " & vbCrLf & _
          "displays several converted Decimal values." & vbCrLf) & vbCrLf
      outputBlock.Text &= String.Format(formatter, "Decimal argument", _
          "Long/exception", "UInt64/exception") & vbCrLf
      outputBlock.Text &= String.Format(formatter, "----------------", _
          "--------------", "----------------") & vbCrLf

      ' Convert Decimal values and display the results.
      DecimalToU_Int64(outputBlock, 123D)
      DecimalToU_Int64(outputBlock, New Decimal(123000, 0, 0, False, 3))
      DecimalToU_Int64(outputBlock, 123.999D)
      DecimalToU_Int64(outputBlock, 18446744073709551615.999D)
      DecimalToU_Int64(outputBlock, 18446744073709551616D)
      DecimalToU_Int64(outputBlock, 9223372036854775807.999D)
      DecimalToU_Int64(outputBlock, 9223372036854775808D)
      DecimalToU_Int64(outputBlock, -0.999D)
      DecimalToU_Int64(outputBlock, -1D)
      DecimalToU_Int64(outputBlock, -9223372036854775808.999D)
      DecimalToU_Int64(outputBlock, -9223372036854775809D)
   End Sub
End Module

' 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      UInt64/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
// 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
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.