Decimal Constructor (Int32, Int32, Int32, Boolean, Byte)
Initializes a new instance of Decimal from parameters specifying the instance's constituent parts.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- lo
- Type: System::Int32
The low 32 bits of a 96-bit integer.
- mid
- Type: System::Int32
The middle 32 bits of a 96-bit integer.
- hi
- Type: System::Int32
The high 32 bits of a 96-bit integer.
- isNegative
- Type: System::Boolean
true to indicate a negative number; false to indicate a positive number.
- scale
- Type: System::Byte
A power of 10 ranging from 0 to 28.
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | scale is greater than 28. |
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.
The following code example creates several Decimal numbers using the constructor overload that initializes a Decimal structure with three Int32 value words, a Boolean sign, and a Byte scale factor.
// 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 */
The following example uses the GetBits method to retrieve the component parts of an array. It then uses this array in the call to the Decimal constructor to instantiate a new Decimal value.
Module Example Public Sub Main() Dim values() As Decimal = { 1234.96d, -1234.96d } For Each value In values Dim parts() = Decimal.GetBits(value) Dim sign As Boolean = (parts(3) And &h80000000) <> 0 Dim scale As Byte = CByte((parts(3) >> 16) And &H7F) Dim newValue As New Decimal(parts(0), parts(1), parts(2), sign, scale) Console.WriteLine("{0} --> {1}", value, newValue) Next End Sub End Module ' The example displays the following output: ' 1234.96 --> 1234.96 ' -1234.96 --> -1234.96
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.