次の方法で共有


Double から Decimal への変換

倍精度浮動小数点数を Decimal に変換します。

returnValue = Decimal.op_Explicit(value)
[C#]
public static explicit operator decimal(doublevalue);
[C++]
public: static Decimal op_Explicit(doublevalue);
[JScript]
returnValue = Decimal(value);

[Visual Basic] Visual Basic では、この型で定義されている型変換を使用することができます。ただし、独自に定義することはできません。

[JScript] JScript では、この型で定義されている型変換を使用することができます。ただし、独自に定義することはできません。

引数 [Visual Basic, JScript]

  • value
    倍精度浮動小数点数。

パラメータ [C#, C++]

  • value
    倍精度浮動小数点数。

戻り値

変換された倍精度浮動小数点数を表す Decimal

例外

例外の種類 条件
OverflowException value が MinValue より小さい値か、 MaxValue より大きい値です。

または

value が Double.NaNDouble.PositiveInfinity 、または Double.NegativeInfinity です。

使用例

[Visual Basic, C#, C++] Double to Decimal 変換を使用して Double の値を Decimal の数値に変換するコード例を次に示します。Visual Basic では、この変換を行うのに op_Explicit 演算子が必要です。

 
' Example of the explicit conversion from Double to Decimal.
Imports System
Imports Microsoft.VisualBasic

Module DecimalFromDoubleDemo

    Const formatter As String = "{0,25:E16}{1,33}"
 
    ' 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 Double argument; catch exceptions that are thrown.
    Sub DecimalFromDouble( argument As Double )

        Dim decValue    As Object
          
        ' Convert the Double argument to a Decimal value.
        Try
            decValue = Decimal.op_Explicit( argument )
        Catch ex As Exception
            decValue = GetExceptionType( ex )
        End Try

        ' Display the Decimal.
        Console.WriteLine( formatter, argument, decValue )
    End Sub 
       
    Sub Main( )

        Console.WriteLine( _
            "This example of the explicit conversion from Double " & _
            "to Decimal " & vbCrLf & "generates the following " & _
            "output." & vbCrLf )
        Console.WriteLine( formatter, "Double argument", _
            "Decimal value" )
        Console.WriteLine( formatter, "---------------", _
            "-------------" )
          
        ' Convert Double values and display the results.
        DecimalFromDouble( 1.234567890123E-30 )
        DecimalFromDouble( 1.2345678901234E-25 )
        DecimalFromDouble( 1.23456789012345E-20 )
        DecimalFromDouble( 1.234567890123456E-10 )
        DecimalFromDouble( 1.2345678901234567 )
        DecimalFromDouble( 1.23456789012345678E+12 )
        DecimalFromDouble( 1.234567890123456789E+28 ) 
        DecimalFromDouble( 1.234567890123456789E+30 )
    End Sub 
End Module

' This example of the explicit conversion from Double to Decimal
' generates the following output.
'
'           Double argument                    Decimal value
'           ---------------                    -------------
'   1.2345678901230000E-030                                0
'   1.2345678901233999E-025   0.0000000000000000000000001235
'   1.2345678901234499E-020   0.0000000000000000000123456789
'   1.2345678901234560E-010       0.000000000123456789012346
'   1.2345678901234567E+000                 1.23456789012346
'   1.2345678901234568E+012                 1234567890123.46
'   1.2345678901234568E+028    12345678901234600000000000000
'   1.2345678901234569E+030                OverflowException

[C#] 
// Example of the explicit conversion from double to decimal.
using System;

class DecimalFromDoubleDemo
{
    const string formatter = "{0,25:E16}{1,33}";

    // 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 double argument; catch exceptions that are thrown.
    public static void DecimalFromDouble( double argument )
    {
        object decValue;

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

        Console.WriteLine( formatter, argument, decValue );
    }

    public static void Main( )
    {
        Console.WriteLine( 
            "This example of the explicit conversion from double " +
            "to decimal \ngenerates the following output.\n" );
        Console.WriteLine( formatter, "double argument", 
            "decimal value" );
        Console.WriteLine( formatter, "---------------", 
            "-------------" );

        // Convert double values and display the results.
        DecimalFromDouble( 1.234567890123E-30 );
        DecimalFromDouble( 1.2345678901234E-25 );
        DecimalFromDouble( 1.23456789012345E-20 );
        DecimalFromDouble( 1.234567890123456E-10 );
        DecimalFromDouble( 1.2345678901234567 );
        DecimalFromDouble( 1.23456789012345678E+12 );
        DecimalFromDouble( 1.234567890123456789E+28 );
        DecimalFromDouble( 1.234567890123456789E+30 );
    }
}

/*
This example of the explicit conversion from double to decimal
generates the following output.

          double argument                    decimal value
          ---------------                    -------------
  1.2345678901230000E-030                                0
  1.2345678901233999E-025   0.0000000000000000000000001235
  1.2345678901234499E-020   0.0000000000000000000123456789
  1.2345678901234560E-010       0.000000000123456789012346
  1.2345678901234567E+000                 1.23456789012346
  1.2345678901234568E+012                 1234567890123.46
  1.2345678901234568E+028    12345678901234600000000000000
  1.2345678901234569E+030                OverflowException
*/

[C++] 
// Example of the explicit conversion from double to Decimal.
#using <mscorlib.dll>
using namespace System;

const __wchar_t* formatter = L"{0,25:E16}{1,33}";

// 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 double argument; catch exceptions that are thrown.
void DecimalFromDouble( double argument )
{
    Object* decValue;

    // Convert the double argument to a Decimal value.
    try
    {
        decValue = __box( (Decimal)argument );
    }
    catch( Exception* ex )
    {
        decValue = GetExceptionType( ex );
    }

    Console::WriteLine( formatter, __box( argument ), decValue );
}

void main( )
{
    Console::WriteLine( 
        S"This example of the explicit conversion from double " 
        S"to Decimal \ngenerates the following output.\n" );
    Console::WriteLine( formatter, S"double argument", 
        S"Decimal value" );
    Console::WriteLine( formatter, S"---------------", 
        S"-------------" );

    // Convert double values and display the results.
    DecimalFromDouble( 1.234567890123E-30 );
    DecimalFromDouble( 1.2345678901234E-25 );
    DecimalFromDouble( 1.23456789012345E-20 );
    DecimalFromDouble( 1.234567890123456E-10 );
    DecimalFromDouble( 1.2345678901234567 );
    DecimalFromDouble( 1.23456789012345678E+12 );
    DecimalFromDouble( 1.234567890123456789E+28 );
    DecimalFromDouble( 1.234567890123456789E+30 );
}

/*
This example of the explicit conversion from double to Decimal
generates the following output.

          double argument                    Decimal value
          ---------------                    -------------
  1.2345678901230000E-030                                0
  1.2345678901233999E-025   0.0000000000000000000000001235
  1.2345678901234499E-020   0.0000000000000000000123456789
  1.2345678901234560E-010       0.000000000123456789012346
  1.2345678901234567E+000                 1.23456789012346
  1.2345678901234568E+012                 1234567890123.46
  1.2345678901234568E+028    12345678901234600000000000000
  1.2345678901234569E+030                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, Common Language Infrastructure (CLI) Standard

参照

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