次の方法で共有


Decimal.ToUInt64 メソッド

指定した Decimal の値を、等価の 64 ビット符号なし整数に変換します。

このメソッドは、CLS と互換性がありません。CLS との互換性に関する詳細については 「共通言語仕様の概要」 を参照してください。

<CLSCompliant(False)>
Public Shared Function ToUInt64( _   ByVal d As Decimal _) As UInt64
[C#]
[CLSCompliant(false)]
public static ulong ToUInt64(decimald);
[C++]
[CLSCompliant(false)]
public: static unsigned __int64 ToUInt64(Decimald);
[JScript]
public
   CLSCompliant(false)
static function ToUInt64(d : Decimal) : UInt64;

パラメータ

戻り値

d の値と等価な 64 ビット符号なし整数。

例外

例外の種類 条件
OverflowException d が負の値であるか、 UInt64.MaxValue よりも大きい値です。

解説

戻り値は 10 進値の整数部分です。小数の桁は切り捨てられます。

使用例

[Visual Basic, C#, C++] ToUInt64 メソッドを使用して、 Decimal の数値を UInt64 の値に変換するコード例を次に示します。

 
' Example of the Decimal.ToInt64 and Decimal.ToUInt64 methods.
Imports System
Imports Microsoft.VisualBasic

Module DecimalToU_Int64Demo

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

    ' Get the exception type name; remove the namespace prefix.
    Function GetExceptionType( 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( 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

        Console.WriteLine( formatter, argument, _
            Int64Value, UInt64Value )
    End Sub

    Sub Main( )

        Console.WriteLine( "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 )
        Console.WriteLine( formatter, "Decimal argument", _
            "Long/exception", "UInt64/exception" )
        Console.WriteLine( formatter, "----------------", _
            "--------------", "----------------" )

        ' Convert Decimal values and display the results.
        DecimalToU_Int64( 123D )
        DecimalToU_Int64( New Decimal( 123000, 0, 0, False, 3 ) )
        DecimalToU_Int64( 123.999D )
        DecimalToU_Int64( 18446744073709551615.999D )
        DecimalToU_Int64( 18446744073709551616D )
        DecimalToU_Int64( 9223372036854775807.999D )
        DecimalToU_Int64( 9223372036854775808D )
        DecimalToU_Int64( - 0.999D )
        DecimalToU_Int64( - 1D )
        DecimalToU_Int64( - 9223372036854775808.999D )
        DecimalToU_Int64( - 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

[C#] 
// Example of the decimal.ToInt64 and decimal.ToUInt64 methods.
using System;

class DecimalToU_Int64Demo
{
    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( 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 );
        }

        Console.WriteLine( formatter, argument, 
            Int64Value, UInt64Value );
    }

    public static void Main( )
    {
        Console.WriteLine( "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" );
        Console.WriteLine( formatter, "decimal argument", 
            "long/exception", "ulong/exception" );
        Console.WriteLine( formatter, "----------------", 
            "--------------", "---------------" );

        // Convert decimal values and display the results.
        DecimalToU_Int64( 123M );
        DecimalToU_Int64( new decimal( 123000, 0, 0, false, 3 ) );
        DecimalToU_Int64( 123.999M );
        DecimalToU_Int64( 18446744073709551615.999M );
        DecimalToU_Int64( 18446744073709551616M );
        DecimalToU_Int64( 9223372036854775807.999M );
        DecimalToU_Int64( 9223372036854775808M );
        DecimalToU_Int64( - 0.999M );
        DecimalToU_Int64( - 1M );
        DecimalToU_Int64( - 9223372036854775808.999M );
        DecimalToU_Int64( - 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
*/

[C++] 
// Example of the Decimal::ToInt64 and Decimal::ToUInt64 methods.
#using <mscorlib.dll>
using namespace System;

const __wchar_t* formatter = L"{0,25}{1,22}{2,22}";

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

// Convert the Decimal argument; catch exceptions that are thrown.
void DecimalToU_Int64( Decimal argument )
{
    Object* Int64Value;
    Object* UInt64Value;

    // Convert the argument to an __int64 value.
    try
    {
        Int64Value = __box( Decimal::ToInt64( argument ) );
    }
    catch( Exception* ex )
    {
        Int64Value = GetExceptionType( ex );
    }

    // Convert the argument to an unsigned __int64 value.
    try
    {
        UInt64Value = __box( Decimal::ToUInt64( argument ) );
    }
    catch( Exception* ex )
    {
        UInt64Value = GetExceptionType( ex );
    }

    Console::WriteLine( formatter, __box( argument ), 
        Int64Value, UInt64Value );
}

void main( )
{
    Console::WriteLine( S"This example of the \n" 
        S"  Decimal::ToInt64( Decimal ) and \n" 
        S"  Decimal::ToUInt64( Decimal ) \nmethods " 
        S"generates the following output. It \ndisplays " 
        S"several converted Decimal values.\n" );
    Console::WriteLine( formatter, S"Decimal argument", 
        S"__int64/exception", S"unsigned __int64" );
    Console::WriteLine( formatter, S"----------------", 
        S"-----------------", S"----------------" );

    // Convert Decimal values and display the results.
    DecimalToU_Int64( Decimal::Parse( "123" ) );
    DecimalToU_Int64( Decimal( 123000, 0, 0, false, 3 ) );
    DecimalToU_Int64( Decimal::Parse( "123.999" ) );
    DecimalToU_Int64( Decimal::Parse( "18446744073709551615.999" ) );
    DecimalToU_Int64( Decimal::Parse( "18446744073709551616" ) );
    DecimalToU_Int64( Decimal::Parse( "9223372036854775807.999" ) );
    DecimalToU_Int64( Decimal::Parse( "9223372036854775808" ) );
    DecimalToU_Int64( Decimal::Parse( "-0.999" ) );
    DecimalToU_Int64( Decimal::Parse( "-1" ) );
    DecimalToU_Int64( Decimal::Parse( "-9223372036854775808.999" ) );
    DecimalToU_Int64( Decimal::Parse( "-9223372036854775809" ) );
}

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

         Decimal argument     __int64/exception      unsigned __int64
         ----------------     -----------------      ----------------
                      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
*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

Decimal 構造体 | Decimal メンバ | System 名前空間 | UInt64