Export (0) Print
Expand All

Decimal Constructor (Int32, Int32, Int32, Boolean, Byte)

Initializes a new instance of Decimal from parameters specifying the instance's constituent parts.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public Decimal (
	int lo,
	int mid,
	int hi,
	bool isNegative,
	byte 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
)

Parameters

lo

The low 32 bits of a 96-bit integer.

mid

The middle 32 bits of a 96-bit integer.

hi

The high 32 bits of a 96-bit integer.

isNegative

The sign of the number; 1 is negative, 0 is positive.

scale

A power of 10 ranging from 0 to 28.

Exception typeCondition

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

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft