次の方法で共有


Decimal コンストラクタ (Int32, Int32, Int32, Boolean, Byte)

Decimal の新しいインスタンスの値を、そのインスタンスの構成部分を指定するパラメータに従って初期化します。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)

構文

'宣言
Public Sub New ( _
    lo As Integer, _
    mid As Integer, _
    hi As Integer, _
    isNegative As Boolean, _
    scale As Byte _
)
'使用
Dim lo As Integer
Dim mid As Integer
Dim hi As Integer
Dim isNegative As Boolean
Dim scale As Byte

Dim instance As New Decimal(lo, mid, hi, isNegative, scale)
public Decimal (
    int lo,
    int mid,
    int hi,
    bool isNegative,
    byte scale
)
public:
Decimal (
    int lo, 
    int mid, 
    int hi, 
    bool isNegative, 
    unsigned char scale
)
public Decimal (
    int lo, 
    int mid, 
    int hi, 
    boolean isNegative, 
    byte scale
)
public function Decimal (
    lo : int, 
    mid : int, 
    hi : int, 
    isNegative : boolean, 
    scale : byte
)

パラメータ

  • lo
    96 ビット整数の下位 32 ビット。
  • mid
    96 ビット整数の中位 32 ビット。
  • hi
    96 ビット整数の上位 32 ビット。
  • isNegative
    符号。1 は負、0 は正。
  • scale
    0 から 28 までの 10 の指数。

例外

例外の種類 条件

ArgumentOutOfRangeException

scale が 28 より大きい値です。

解説

Decimal 数値のバイナリ表現は、1 ビットの符号、96 ビットの整数、および整数値を除算し、小数部を指定するために使用するスケール ファクタから構成されます。スケール ファクタは黙示的に数値 10 になり、0 から 28 の範囲の指数で累乗されます。

使用例

Decimal 構造体を Int32 値の 3 つの単語、Boolean 符合、および Byte のスケール ファクタで初期化するコンストラクタのオーバーロードを使用して、複数の Decimal 数値を作成するコード例を次に示します。

' Example of the Decimal( Integer, Integer, Integer, Boolean, Byte ) 
' constructor.
Imports System
Imports Microsoft.VisualBasic

Module DecimalCtorIIIBByDemo

    ' 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

    ' Create a Decimal object and display its value.
    Sub CreateDecimal( low As Integer, mid As Integer, _
        high As Integer, isNeg As Boolean, scale as Byte )

        ' Format the constructor for display.
        Dim ctor As String = String.Format( _
            "Decimal( {0}, {1}, {2}, {3}, {4} )", _
            low, mid, high, isNeg, scale )
        Dim valOrExc As String

        ' Construct the Decimal value.
        Try
            Dim decimalNum As New Decimal( _
                low, mid, high, isNeg, scale )

            ' Format and save the Decimal value.
            valOrExc = decimalNum.ToString( )

        ' Save the exception type if an exception was thrown.
        Catch ex As Exception
            valOrExc =  GetExceptionType( ex ) 
        End Try

        ' Display the constructor and Decimal value or exception.
        Dim ctorLen = 76 - valOrExc.Length
        If ctorLen > ctor.Length Then

            ' Display the data on one line if it will fit.
            Console.WriteLine( "{0}{1}", ctor.PadRight( ctorLen ), _
                valOrExc )

        ' Otherwise, display the data on two lines.
        Else
            Console.WriteLine( "{0}", ctor )
            Console.WriteLine( "{0,76}", valOrExc )
        End If
    End Sub
    
    Sub Main( )

        Console.WriteLine( _
            "This example of the Decimal( Integer, Integer, " & _
            "Integer, Boolean, Byte ) " & vbCrLf & "constructor " & _
            "generates the following output." & vbCrLf )
        Console.WriteLine( "{0,-38}{1,38}", "Constructor", _
            "Value or Exception" )
        Console.WriteLine( "{0,-38}{1,38}", "-----------", _
            "------------------" )

        ' Construct Decimal objects from the component fields.
        CreateDecimal( 0, 0, 0, False, 0 )                
        CreateDecimal( 0, 0, 0, False, 27 )                
        CreateDecimal( 0, 0, 0, True, 0 )                
        CreateDecimal( 1000000000, 0, 0, False, 0 )                
        CreateDecimal( 0, 1000000000, 0, False, 0 )                
        CreateDecimal( 0, 0, 1000000000, False, 0 )                
        CreateDecimal( 1000000000, 1000000000, 1000000000, False, 0 )
        CreateDecimal( -1, -1, -1, False, 0 )                
        CreateDecimal( -1, -1, -1, True, 0 )                
        CreateDecimal( -1, -1, -1, False, 15 )                
        CreateDecimal( -1, -1, -1, False, 28 )                
        CreateDecimal( -1, -1, -1, False, 29 )                
        CreateDecimal( Integer.MaxValue, 0, 0, False, 18 )                
        CreateDecimal( Integer.MaxValue, 0, 0, False, 28 )                
        CreateDecimal( Integer.MaxValue, 0, 0, True, 28 )                
    End Sub 
End Module 

' This example of the Decimal( Integer, Integer, Integer, Boolean, Byte )
' constructor generates the following output.
' 
' Constructor                                               Value or Exception
' -----------                                               ------------------
' Decimal( 0, 0, 0, False, 0 )                                               0
' Decimal( 0, 0, 0, False, 27 )                                              0
' Decimal( 0, 0, 0, True, 0 )                                                0
' Decimal( 1000000000, 0, 0, False, 0 )                             1000000000
' Decimal( 0, 1000000000, 0, False, 0 )                    4294967296000000000
' Decimal( 0, 0, 1000000000, False, 0 )          18446744073709551616000000000
' Decimal( 1000000000, 1000000000, 1000000000, False, 0 )
'                                                18446744078004518913000000000
' Decimal( -1, -1, -1, False, 0 )                79228162514264337593543950335
' Decimal( -1, -1, -1, True, 0 )                -79228162514264337593543950335
' Decimal( -1, -1, -1, False, 15 )              79228162514264.337593543950335
' Decimal( -1, -1, -1, False, 28 )              7.9228162514264337593543950335
' Decimal( -1, -1, -1, False, 29 )                 ArgumentOutOfRangeException
' Decimal( 2147483647, 0, 0, False, 18 )                  0.000000002147483647
' Decimal( 2147483647, 0, 0, False, 28 )        0.0000000000000000002147483647
' Decimal( 2147483647, 0, 0, True, 28 )        -0.0000000000000000002147483647
// Example of the decimal( int, int, int, bool, byte ) constructor.
using System;

class DecimalCtorIIIBByDemo
{
    // 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 );
    }

    // Create a decimal object and display its value.
    public static void CreateDecimal( int low, int mid, int high, 
        bool isNeg, byte scale )
    {
        // Format the constructor for display.
        string ctor = String.Format( 
            "decimal( {0}, {1}, {2}, {3}, {4} )", 
            low, mid, high, isNeg, scale );
        string valOrExc;

        try
        {
            // Construct the decimal value.
            decimal decimalNum = new decimal( 
                low, mid, high, isNeg, scale );

            // Format and save the decimal value.
            valOrExc = decimalNum.ToString( );
        }
        catch( Exception ex )
        {
            // Save the exception type if an exception was thrown.
            valOrExc = GetExceptionType( ex );
        }

        // Display the constructor and decimal value or exception.
        int ctorLen = 76 - valOrExc.Length;

        // Display the data on one line if it will fit.
        if ( ctorLen > ctor.Length )
            Console.WriteLine( "{0}{1}", ctor.PadRight( ctorLen ), 
                valOrExc );

        // Otherwise, display the data on two lines.
        else
        {
            Console.WriteLine( "{0}", ctor );
            Console.WriteLine( "{0,76}", valOrExc );
        }
    }
    
    public static void Main( )
    {

        Console.WriteLine( "This example of the decimal( int, int, " +
            "int, bool, byte ) \nconstructor " +
            "generates the following output.\n" );
        Console.WriteLine( "{0,-38}{1,38}", "Constructor", 
            "Value or Exception" );
        Console.WriteLine( "{0,-38}{1,38}", "-----------", 
            "------------------" );

        // Construct decimal objects from the component fields.
        CreateDecimal( 0, 0, 0, false, 0 );
        CreateDecimal( 0, 0, 0, false, 27 );
        CreateDecimal( 0, 0, 0, true, 0 );
        CreateDecimal( 1000000000, 0, 0, false, 0 );
        CreateDecimal( 0, 1000000000, 0, false, 0 );
        CreateDecimal( 0, 0, 1000000000, false, 0 );
        CreateDecimal( 1000000000, 1000000000, 1000000000, false, 0 );
        CreateDecimal( -1, -1, -1, false, 0 );
        CreateDecimal( -1, -1, -1, true, 0 );
        CreateDecimal( -1, -1, -1, false, 15 );
        CreateDecimal( -1, -1, -1, false, 28 );
        CreateDecimal( -1, -1, -1, false, 29 );
        CreateDecimal( int.MaxValue, 0, 0, false, 18 );
        CreateDecimal( int.MaxValue, 0, 0, false, 28 );
        CreateDecimal( int.MaxValue, 0, 0, true, 28 );
    }
}

/*
This example of the decimal( int, int, int, bool, byte )
constructor generates the following output.

Constructor                                               Value or Exception
-----------                                               ------------------
decimal( 0, 0, 0, False, 0 )                                               0
decimal( 0, 0, 0, False, 27 )                                              0
decimal( 0, 0, 0, True, 0 )                                                0
decimal( 1000000000, 0, 0, False, 0 )                             1000000000
decimal( 0, 1000000000, 0, False, 0 )                    4294967296000000000
decimal( 0, 0, 1000000000, False, 0 )          18446744073709551616000000000
decimal( 1000000000, 1000000000, 1000000000, False, 0 )
                                               18446744078004518913000000000
decimal( -1, -1, -1, False, 0 )                79228162514264337593543950335
decimal( -1, -1, -1, True, 0 )                -79228162514264337593543950335
decimal( -1, -1, -1, False, 15 )              79228162514264.337593543950335
decimal( -1, -1, -1, False, 28 )              7.9228162514264337593543950335
decimal( -1, -1, -1, False, 29 )                 ArgumentOutOfRangeException
decimal( 2147483647, 0, 0, False, 18 )                  0.000000002147483647
decimal( 2147483647, 0, 0, False, 28 )        0.0000000000000000002147483647
decimal( 2147483647, 0, 0, True, 28 )        -0.0000000000000000002147483647
*/
// Example of the Decimal( int, int, int, bool, unsigned char ) 
// constructor.
using namespace System;

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


// Create a Decimal object and display its value.
void CreateDecimal( int low, int mid, int high, bool isNeg, unsigned char scale )
{
   
   // Format the constructor for display.
   array<Object^>^boxedParams = gcnew array<Object^>(5);
   boxedParams[ 0 ] = low;
   boxedParams[ 1 ] = mid;
   boxedParams[ 2 ] = high;
   boxedParams[ 3 ] = isNeg;
   boxedParams[ 4 ] = scale;
   String^ ctor = String::Format( "Decimal( {0}, {1}, {2}, {3}, {4} )", boxedParams );
   String^ valOrExc;
   try
   {
      
      // Construct the Decimal value.
      Decimal decimalNum = Decimal(low,mid,high,isNeg,scale);
      
      // Format and save the Decimal value.
      valOrExc = decimalNum.ToString();
   }
   catch ( Exception^ ex ) 
   {
      
      // Save the exception type if an exception was thrown.
      valOrExc = GetExceptionType( ex );
   }

   
   // Display the constructor and Decimal value or exception.
   int ctorLen = 76 - valOrExc->Length;
   
   // Display the data on one line if it will fit.
   if ( ctorLen > ctor->Length )
      Console::WriteLine( "{0}{1}", ctor->PadRight( ctorLen ), valOrExc );
   // Otherwise, display the data on two lines.
   else
   {
      Console::WriteLine( "{0}", ctor );
      Console::WriteLine( "{0,76}", valOrExc );
   }
}

int main()
{
   Console::WriteLine( "This example of the Decimal( int, int, "
   "int, bool, unsigned char ) \nconstructor "
   "generates the following output.\n" );
   Console::WriteLine( "{0,-38}{1,38}", "Constructor", "Value or Exception" );
   Console::WriteLine( "{0,-38}{1,38}", "-----------", "------------------" );
   
   // Construct Decimal objects from double values.
   CreateDecimal( 0, 0, 0, false, 0 );
   CreateDecimal( 0, 0, 0, false, 27 );
   CreateDecimal( 0, 0, 0, true, 0 );
   CreateDecimal( 1000000000, 0, 0, false, 0 );
   CreateDecimal( 0, 1000000000, 0, false, 0 );
   CreateDecimal( 0, 0, 1000000000, false, 0 );
   CreateDecimal( 1000000000, 1000000000, 1000000000, false, 0 );
   CreateDecimal(  -1, -1, -1, false, 0 );
   CreateDecimal(  -1, -1, -1, true, 0 );
   CreateDecimal(  -1, -1, -1, false, 15 );
   CreateDecimal(  -1, -1, -1, false, 28 );
   CreateDecimal(  -1, -1, -1, false, 29 );
   CreateDecimal( Int32::MaxValue, 0, 0, false, 18 );
   CreateDecimal( Int32::MaxValue, 0, 0, false, 28 );
   CreateDecimal( Int32::MaxValue, 0, 0, true, 28 );
}

/*
This example of the Decimal( int, int, int, bool, unsigned char )
constructor generates the following output.

Constructor                                               Value or Exception
-----------                                               ------------------
Decimal( 0, 0, 0, False, 0 )                                               0
Decimal( 0, 0, 0, False, 27 )                                              0
Decimal( 0, 0, 0, True, 0 )                                                0
Decimal( 1000000000, 0, 0, False, 0 )                             1000000000
Decimal( 0, 1000000000, 0, False, 0 )                    4294967296000000000
Decimal( 0, 0, 1000000000, False, 0 )          18446744073709551616000000000
Decimal( 1000000000, 1000000000, 1000000000, False, 0 )
                                               18446744078004518913000000000
Decimal( -1, -1, -1, False, 0 )                79228162514264337593543950335
Decimal( -1, -1, -1, True, 0 )                -79228162514264337593543950335
Decimal( -1, -1, -1, False, 15 )              79228162514264.337593543950335
Decimal( -1, -1, -1, False, 28 )              7.9228162514264337593543950335
Decimal( -1, -1, -1, False, 29 )                 ArgumentOutOfRangeException
Decimal( 2147483647, 0, 0, False, 18 )                  0.000000002147483647
Decimal( 2147483647, 0, 0, False, 28 )        0.0000000000000000002147483647
Decimal( 2147483647, 0, 0, True, 28 )        -0.0000000000000000002147483647
*/
// Example of the decimal( int, int, int, bool, byte ) constructor.
import System.* ;

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

    // Create a decimal object and display its value.
    public static void CreateDecimal(int low, int mid, 
        int high, boolean isNeg, ubyte scale)
    {
        // Format the constructor for display.
        String ctor = String.Format("decimal( {0}, {1}, {2}, {3}, {4} )",
                    new Object[] { new Integer(low), new Integer(mid),
                    new Integer(high), (System.Boolean)(isNeg), 
                    new Integer(scale) });
        String valOrExc;

        try {
            // Construct the decimal value.
            System.Decimal decimalNum = new System.Decimal(low, mid, high, 
                                        isNeg, scale);

            // Format and save the decimal value.
            valOrExc = decimalNum.ToString();
        }
        catch (System.Exception ex)    {
            // Save the exception type if an exception was thrown.
            valOrExc = System.Convert.ToString(GetExceptionType(ex));
        }

        // Display the constructor and decimal value or exception.
        int ctorLen = 76 - valOrExc.get_Length();

        // Display the data on one line if it will fit.
        if (ctorLen > ctor.get_Length()) {
            Console.WriteLine("{0}{1}", ctor.PadRight(ctorLen), valOrExc);
        }
        // Otherwise, display the data on two lines.
        else {
            Console.WriteLine("{0}", ctor);
            Console.WriteLine("{0,76}", valOrExc);
        }
    } //CreateDecimal

    public static void main(String[] args)
    {
        Console.WriteLine(("This example of the decimal( int, int, " 
            + "int, bool, byte ) \nconstructor "
            + "generates the following output.\n"));
        Console.WriteLine("{0,-38}{1,38}", "Constructor", "Value or Exception");
        Console.WriteLine("{0,-38}{1,38}", "-----------", "------------------");

        // Construct decimal objects from the component fields.
        CreateDecimal(0, 0, 0, false, (ubyte)(0));
        CreateDecimal(0, 0, 0, false, (ubyte)27);
        CreateDecimal(0, 0, 0, true, (ubyte)0);
        CreateDecimal(1000000000, 0, 0, false, (ubyte)0);
        CreateDecimal(0, 1000000000, 0, false, (ubyte)0);
        CreateDecimal(0, 0, 1000000000, false, (ubyte)0);
        CreateDecimal(1000000000, 1000000000, 1000000000, false, (ubyte)0);
        CreateDecimal(-1, -1, -1, false, (ubyte)0);
        CreateDecimal(-1, -1, -1, true, (ubyte)0);
        CreateDecimal(-1, -1, -1, false, (ubyte)15);
        CreateDecimal(-1, -1, -1, false, (ubyte)28);
        CreateDecimal(-1, -1, -1, false, (ubyte)29);
        CreateDecimal(Integer.MAX_VALUE, 0, 0, false, (ubyte)18);
        CreateDecimal(Integer.MAX_VALUE, 0, 0, false, (ubyte)28);
        CreateDecimal(Integer.MAX_VALUE, 0, 0, true, (ubyte)28);
    } //main
} //DecimalCtorIIIBByDemo

/*
This example of the decimal( int, int, int, bool, byte )
constructor generates the following output.

Constructor                                               Value or Exception
-----------                                               ------------------
decimal( 0, 0, 0, False, 0 )                                               0
decimal( 0, 0, 0, False, 27 )                                              0
decimal( 0, 0, 0, True, 0 )                                                0
decimal( 1000000000, 0, 0, False, 0 )                             1000000000
decimal( 0, 1000000000, 0, False, 0 )                    4294967296000000000
decimal( 0, 0, 1000000000, False, 0 )          18446744073709551616000000000
decimal( 1000000000, 1000000000, 1000000000, False, 0 )
                                               18446744078004518913000000000
decimal( -1, -1, -1, False, 0 )                79228162514264337593543950335
decimal( -1, -1, -1, True, 0 )                -79228162514264337593543950335
decimal( -1, -1, -1, False, 15 )              79228162514264.337593543950335
decimal( -1, -1, -1, False, 28 )              7.9228162514264337593543950335
decimal( -1, -1, -1, False, 29 )                 ArgumentOutOfRangeException
decimal( 2147483647, 0, 0, False, 18 )                  0.000000002147483647
decimal( 2147483647, 0, 0, False, 28 )        0.0000000000000000002147483647
decimal( 2147483647, 0, 0, True, 28 )        -0.0000000000000000002147483647
*/

プラットフォーム

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。

バージョン情報

.NET Framework

サポート対象 : 2.0、1.1、1.0

.NET Compact Framework

サポート対象 : 2.0、1.0

参照

関連項目

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