This topic has not yet been rated - Rate this topic

# Decimal Constructor (array<Int32>)

.NET Framework 4.5

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
```public:
Decimal(
array<int>^ bits
)
```

#### Parameters

bits
Type: array<System::Int32>

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

ExceptionCondition
ArgumentNullException

bits is nullptr.

ArgumentException

The length of the bits is not 4.

-or-

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

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, which 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.

The following code example creates several Decimal numbers using the constructor overload that initializes a Decimal structure with an array of four Int32 values.

```// Example of the Decimal( int __gc [ ] ) 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( array<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::Concat( ctor, String::Format( ", 0x{0:X}", bits[ index ] ) );
ctor = String::Concat( ctor, " } )" );
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( "{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 __gc [ ] ) "
"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.
array<Int32>^zero = {0,0,0,0};
CreateDecimal( zero );
array<Int32>^arrayShort = {0,0,0};
CreateDecimal( arrayShort );
array<Int32>^arrayLong = {0,0,0,0,0};
CreateDecimal( arrayLong );
array<Int32>^word0Data = {1000000000,0,0,0};
CreateDecimal( word0Data );
array<Int32>^word1Data = {0,1000000000,0,0};
CreateDecimal( word1Data );
array<Int32>^word2Data = {0,0,1000000000,0};
CreateDecimal( word2Data );
array<Int32>^word3Data = {0,0,0,1000000000};
CreateDecimal( word3Data );
array<Int32>^decMax = { -1,-1,-1,0};
CreateDecimal( decMax );
array<Int32>^decMin = { -1,-1,-1,0x80000000};
CreateDecimal( decMin );
array<Int32>^fracDig16 = { -1,0,0,0x100000};
CreateDecimal( fracDig16 );
array<Int32>^fracDig28 = { -1,0,0,0x1C0000};
CreateDecimal( fracDig28 );
array<Int32>^fracDig29 = { -1,0,0,0x1D0000};
CreateDecimal( fracDig29 );
array<Int32>^reserved = { -1,0,0,0x1C0001};
CreateDecimal( reserved );
array<Int32>^Same4Words = {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
*/
```

#### .NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

#### .NET Framework Client Profile

Supported in: 4, 3.5 SP1

#### Portable Class Library

Supported in: Portable Class Library

#### .NET for Windows Store apps

Supported in: Windows 8

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.