This documentation is archived and is not being maintained.

Decimal Constructor (Int32[])

.NET Framework 1.1

Initializes a new instance of Decimal to a decimal value represented in binary and contained in a specified array.

[Visual Basic]
Public Sub New( _
   ByVal bits() As Integer _
)
[C#]
public Decimal(
   int[] bits
);
[C++]
public: Decimal(
   int bits __gc[]
);
[JScript]
public function Decimal(
   bits : int[]
);

Parameters

bits
An array of 32-bit signed integers containing a representation of a decimal value.

Exceptions

Exception Type Condition
ArgumentNullException bits is a null reference (Nothing in Visual Basic).
ArgumentException The length of the bits is not 4.

-or-

The representation of the decimal value in bits is not valid.

Remarks

The binary representation of a Decimal number consists of a 1-bit sign, a 96-bit integer number, and a scaling factor used to divide the integer number and specify what portion of it is a decimal fraction. The scaling factor is implicitly the number 10, raised to an exponent ranging from 0 to 28.

bits is a four-element long array of 32-bit signed integers.

bits [0], bits [1], and bits [2] contain the low, middle, and high 32 bits of the 96-bit integer number.

bits [3] contains the scale factor and sign, and consists of following parts:

Bits 0 to 15, the lower word, are unused and must be zero.

Bits 16 to 23 must contain an exponent between 0 and 28, that indicates the power of 10 to divide the integer number.

Bits 24 to 30 are unused and must be zero.

Bit 31 contains the sign; 0 meaning positive, and 1 meaning negative.

A numeric value might have several possible binary representations; all are equally valid and numerically equivalent. Note that the bit representation differentiates between negative and positive zero. These values are treated as being equal in all operations.

Example

[Visual Basic, C#, C++] The following code example creates several Decimal numbers using the constructor overload that initializes a Decimal structure with an array of four Int32 values.

[Visual Basic] 
' Example of the Decimal( Integer( ) ) constructor.
Imports System
Imports Microsoft.VisualBasic

Module DecimalCtorIArrDemo

    ' 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( ByVal bits( ) As Integer )

        ' Format and save the constructor.
        Dim ctor As String = String.Format( "Decimal( {{ &H{0:X}", bits( 0 ) )
        Dim valOrExc As String
        Dim index As Integer
        For index = 1 to bits.Length - 1
            ctor &= String.Format( ", &H{0:X}", bits( index ) )
        Next index
        ctor &= " } )"

        ' Construct the Decimal value.
        Try
            Dim decimalNum As New Decimal( bits )

            ' Format the Decimal value for display.
            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 As Integer = 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( ) ) constructor " & _
            vbCrLf & "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 Integer arrays.
        CreateDecimal( New Integer( ) { 0, 0, 0, 0 } )
        CreateDecimal( New Integer( ) { 0, 0, 0 } )
        CreateDecimal( New Integer( ) { 0, 0, 0, 0, 0 } )
        CreateDecimal( New Integer( ) { 1000000000, 0, 0, 0 } )
        CreateDecimal( New Integer( ) { 0, 1000000000, 0, 0 } )
        CreateDecimal( New Integer( ) { 0, 0, 1000000000, 0 } )
        CreateDecimal( New Integer( ) { 0, 0, 0, 1000000000 } )
        CreateDecimal( New Integer( ) { -1, -1, -1, 0 } )
        CreateDecimal( New Integer( ) { -1, -1, -1, &H80000000 } )
        CreateDecimal( New Integer( ) { -1, 0, 0, &H100000 } )
        CreateDecimal( New Integer( ) { -1, 0, 0, &H1C0000 } )
        CreateDecimal( New Integer( ) { -1, 0, 0, &H1D0000 } )
        CreateDecimal( New Integer( ) { -1, 0, 0, &H1C0001 } )
        CreateDecimal( New Integer( ) _
            { &HF0000, &HF0000, &HF0000, &HF0000 } )
    End Sub 
End Module 

' This example of the Decimal( Integer( ) ) constructor
' generates the following output.
' 
' Constructor                                               Value or Exception
' -----------                                               ------------------
' Decimal( { &H0, &H0, &H0, &H0 } )                                          0
' Decimal( { &H0, &H0, &H0 } )                               ArgumentException
' Decimal( { &H0, &H0, &H0, &H0, &H0 } )                     ArgumentException
' Decimal( { &H3B9ACA00, &H0, &H0, &H0 } )                          1000000000
' Decimal( { &H0, &H3B9ACA00, &H0, &H0 } )                 4294967296000000000
' Decimal( { &H0, &H0, &H3B9ACA00, &H0 } )       18446744073709551616000000000
' Decimal( { &H0, &H0, &H0, &H3B9ACA00 } )                   ArgumentException
' Decimal( { &HFFFFFFFF, &HFFFFFFFF, &HFFFFFFFF, &H0 } )
'                                                79228162514264337593543950335
' Decimal( { &HFFFFFFFF, &HFFFFFFFF, &HFFFFFFFF, &H80000000 } )
'                                               -79228162514264337593543950335
' Decimal( { &HFFFFFFFF, &H0, &H0, &H100000 } )             0.0000004294967295
' Decimal( { &HFFFFFFFF, &H0, &H0, &H1C0000 } ) 0.0000000000000000004294967295
' Decimal( { &HFFFFFFFF, &H0, &H0, &H1D0000 } )              ArgumentException
' Decimal( { &HFFFFFFFF, &H0, &H0, &H1C0001 } )              ArgumentException
' Decimal( { &HF0000, &HF0000, &HF0000, &HF0000 } )
'                                                  18133887298.441562272235520

[C#] 
// Example of the decimal( int[ ] ) constructor.
using System;

class DecimalCtorIArrDemo
{
    // 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[ ] bits )
    {
        // Format the constructor for display.
        string ctor = String.Format( 
            "decimal( {{ 0x{0:X}", bits[ 0 ] );
        string valOrExc;
        
        for( int index = 1; index < bits.Length; index++ )
            ctor += String.Format( ", 0x{0:X}", bits[ index ] );
        ctor += " } )";

        try
        {
            // Construct the decimal value.
            decimal decimalNum = new decimal( bits );

            // Format the decimal value for display.
            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[ ] ) constructor " +
            "\ngenerates the following output.\n" );
        Console.WriteLine( "{0,-38}{1,38}", "Constructor", 
            "Value or Exception" );
        Console.WriteLine( "{0,-38}{1,38}", "-----------", 
            "------------------" );

        // Construct decimal objects from integer arrays.
        CreateDecimal( new int[ ] { 0, 0, 0, 0 } );
        CreateDecimal( new int[ ] { 0, 0, 0 } );
        CreateDecimal( new int[ ] { 0, 0, 0, 0, 0 } );
        CreateDecimal( new int[ ] { 1000000000, 0, 0, 0 } );
        CreateDecimal( new int[ ] { 0, 1000000000, 0, 0 } );
        CreateDecimal( new int[ ] { 0, 0, 1000000000, 0 } );
        CreateDecimal( new int[ ] { 0, 0, 0, 1000000000 } );
        CreateDecimal( new int[ ] { -1, -1, -1, 0 } );
        CreateDecimal( new int[ ] 
            { -1, -1, -1, unchecked( (int)0x80000000 ) } );
        CreateDecimal( new int[ ] { -1, 0, 0, 0x100000 } );
        CreateDecimal( new int[ ] { -1, 0, 0, 0x1C0000 } );
        CreateDecimal( new int[ ] { -1, 0, 0, 0x1D0000 } );
        CreateDecimal( new int[ ] { -1, 0, 0, 0x1C0001 } );
        CreateDecimal( new int[ ] 
            { 0xF0000, 0xF0000, 0xF0000, 0xF0000 } );
    }
}

/*
This example of the decimal( int[ ] ) constructor
generates the following output.

Constructor                                               Value or Exception
-----------                                               ------------------
decimal( { 0x0, 0x0, 0x0, 0x0 } )                                          0
decimal( { 0x0, 0x0, 0x0 } )                               ArgumentException
decimal( { 0x0, 0x0, 0x0, 0x0, 0x0 } )                     ArgumentException
decimal( { 0x3B9ACA00, 0x0, 0x0, 0x0 } )                          1000000000
decimal( { 0x0, 0x3B9ACA00, 0x0, 0x0 } )                 4294967296000000000
decimal( { 0x0, 0x0, 0x3B9ACA00, 0x0 } )       18446744073709551616000000000
decimal( { 0x0, 0x0, 0x0, 0x3B9ACA00 } )                   ArgumentException
decimal( { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x0 } )
                                               79228162514264337593543950335
decimal( { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x80000000 } )
                                              -79228162514264337593543950335
decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x100000 } )             0.0000004294967295
decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x1C0000 } ) 0.0000000000000000004294967295
decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x1D0000 } )              ArgumentException
decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x1C0001 } )              ArgumentException
decimal( { 0xF0000, 0xF0000, 0xF0000, 0xF0000 } )
                                                 18133887298.441562272235520
*/

[C++] 
// Example of the Decimal( int __gc [ ] ) constructor.
#using <mscorlib.dll>
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 bits __gc [ ] )
{
    // Format the constructor for display.
    String* ctor = String::Format( 
        S"Decimal( {{ 0x{0:X}", __box( bits[ 0 ] ) );
    String* valOrExc;
    
    for( int index = 1; index < bits->Length; index++ )
        ctor = String::Concat( ctor, 
            String::Format( S", 0x{0:X}", __box( bits[ index ] ) ) );
    ctor = String::Concat( ctor, S" } )" );

    try
    {
        // Construct the Decimal value.
        Decimal decimalNum = Decimal( bits );

        // Format the Decimal value for display.
        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( S"{0}{1}", ctor->PadRight( ctorLen ), 
            valOrExc );

    // Otherwise, display the data on two lines.
    else
    {
        Console::WriteLine( S"{0}", ctor );
        Console::WriteLine( S"{0,76}", valOrExc );
    }
}

void main( )
{
    Console::WriteLine( 
        S"This example of the Decimal( int __gc [ ] ) " 
        S"constructor \ngenerates the following output.\n" );
    Console::WriteLine( S"{0,-38}{1,38}", S"Constructor", 
        S"Value or Exception" );
    Console::WriteLine( S"{0,-38}{1,38}", S"-----------", 
        S"------------------" );

    // Construct Decimal objects from integer arrays.
    Int32 zero __gc [ ] = { 0, 0, 0, 0 };
    CreateDecimal( zero );
    Int32 arrayShort __gc [ ] = { 0, 0, 0 };
    CreateDecimal( arrayShort );
    Int32 arrayLong __gc [ ] = { 0, 0, 0, 0, 0 };
    CreateDecimal( arrayLong );
    Int32 word0Data __gc [ ] = { 1000000000, 0, 0, 0 } ;
    CreateDecimal( word0Data );
    Int32 word1Data __gc [ ] = { 0, 1000000000, 0, 0 } ;
    CreateDecimal( word1Data );
    Int32 word2Data __gc [ ] = { 0, 0, 1000000000, 0 } ;
    CreateDecimal( word2Data );
    Int32 word3Data __gc [ ] = { 0, 0, 0, 1000000000 } ;
    CreateDecimal( word3Data );
    Int32 decMax __gc [ ] = { -1, -1, -1, 0 } ;
    CreateDecimal( decMax );
    Int32 decMin __gc [ ] = { -1, -1, -1, 0x80000000 } ;
    CreateDecimal( decMin );
    Int32 fracDig16 __gc [ ] = { -1, 0, 0, 0x100000 } ;
    CreateDecimal( fracDig16 );
    Int32 fracDig28 __gc [ ] = { -1, 0, 0, 0x1C0000 } ;
    CreateDecimal( fracDig28 );
    Int32 fracDig29 __gc [ ] = { -1, 0, 0, 0x1D0000 } ;
    CreateDecimal( fracDig29 );
    Int32 reserved __gc [ ] = { -1, 0, 0, 0x1C0001 } ;
    CreateDecimal( reserved );
    Int32 Same4Words __gc [ ] = { 0xF0000, 0xF0000, 0xF0000, 0xF0000 } ;
    CreateDecimal( Same4Words );
}

/*
This example of the Decimal( int __gc [ ] ) constructor
generates the following output.

Constructor                                               Value or Exception
-----------                                               ------------------
Decimal( { 0x0, 0x0, 0x0, 0x0 } )                                          0
Decimal( { 0x0, 0x0, 0x0 } )                               ArgumentException
Decimal( { 0x0, 0x0, 0x0, 0x0, 0x0 } )                     ArgumentException
Decimal( { 0x3B9ACA00, 0x0, 0x0, 0x0 } )                          1000000000
Decimal( { 0x0, 0x3B9ACA00, 0x0, 0x0 } )                 4294967296000000000
Decimal( { 0x0, 0x0, 0x3B9ACA00, 0x0 } )       18446744073709551616000000000
Decimal( { 0x0, 0x0, 0x0, 0x3B9ACA00 } )                   ArgumentException
Decimal( { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x0 } )
                                               79228162514264337593543950335
Decimal( { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x80000000 } )
                                              -79228162514264337593543950335
Decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x100000 } )             0.0000004294967295
Decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x1C0000 } ) 0.0000000000000000004294967295
Decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x1D0000 } )              ArgumentException
Decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x1C0001 } )              ArgumentException
Decimal( { 0xF0000, 0xF0000, 0xF0000, 0xF0000 } )
                                                 18133887298.441562272235520
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Decimal Structure | Decimal Members | System Namespace | Decimal Constructor Overload List | GetBits

Show: