Decimal Constructor (Int32, Int32, Int32, Boolean, Byte)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
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
The sign of the number; 1 is negative, 0 is positive.
- 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, byte ) constructor. using System; class Example { // 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(System.Windows.Controls.TextBlock outputBlock, 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) outputBlock.Text += String.Format("{0}{1}", ctor.PadRight(ctorLen), valOrExc) + "\n"; // Otherwise, display the data on two lines. else { outputBlock.Text += String.Format("{0}", ctor) + "\n"; outputBlock.Text += String.Format("{0,76}", valOrExc) + "\n"; } } public static void Demo(System.Windows.Controls.TextBlock outputBlock) { outputBlock.Text += String.Format("This example of the decimal( int, int, " + "int, bool, byte ) \nconstructor " + "generates the following output.\n") + "\n"; outputBlock.Text += String.Format("{0,-38}{1,38}", "Constructor", "Value or Exception") + "\n"; outputBlock.Text += String.Format("{0,-38}{1,38}", "-----------", "------------------") + "\n"; // Construct decimal objects from the component fields. CreateDecimal(outputBlock, 0, 0, 0, false, 0); CreateDecimal(outputBlock, 0, 0, 0, false, 27); CreateDecimal(outputBlock, 0, 0, 0, true, 0); CreateDecimal(outputBlock, 1000000000, 0, 0, false, 0); CreateDecimal(outputBlock, 0, 1000000000, 0, false, 0); CreateDecimal(outputBlock, 0, 0, 1000000000, false, 0); CreateDecimal(outputBlock, 1000000000, 1000000000, 1000000000, false, 0); CreateDecimal(outputBlock, -1, -1, -1, false, 0); CreateDecimal(outputBlock, -1, -1, -1, true, 0); CreateDecimal(outputBlock, -1, -1, -1, false, 15); CreateDecimal(outputBlock, -1, -1, -1, false, 28); CreateDecimal(outputBlock, -1, -1, -1, false, 29); CreateDecimal(outputBlock, int.MaxValue, 0, 0, false, 18); CreateDecimal(outputBlock, int.MaxValue, 0, 0, false, 28); CreateDecimal(outputBlock, 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 */