Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Decimal Constructor

.NET Framework 1.1

Initializes a new instance of Decimal.

Overload List

Initializes a new instance of Decimal to the value of the specified double-precision floating-point number.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Double)
[C#] public Decimal(double);
[C++] public: Decimal(double);
[JScript] public function Decimal(double);

Initializes a new instance of Decimal to the value of the specified 32-bit signed integer.

Supported by the .NET Compact Framework.

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

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

Supported by the .NET Compact Framework.

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

Initializes a new instance of Decimal to the value of the specified 64-bit signed integer.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Long)
[C#] public Decimal(long);
[C++] public: Decimal(__int64);
[JScript] public function Decimal(long);

Initializes a new instance of Decimal to the value of the specified single-precision floating-point number.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Single)
[C#] public Decimal(float);
[C++] public: Decimal(float);
[JScript] public function Decimal(float);

Initializes a new instance of Decimal to the value of the specified 32-bit unsigned integer. This constructor is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(UInt32)
[C#] public Decimal(uint);
[C++] public: Decimal(unsigned int);
[JScript] public function Decimal(UInt32);

Initializes a new instance of Decimal to the value of the specified 64-bit unsigned integer. This constructor is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(UInt64)
[C#] public Decimal(ulong);
[C++] public: Decimal(unsigned __int64);
[JScript] public function Decimal(UInt64);

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

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Integer, Integer, Integer, Boolean, Byte)
[C#] public Decimal(int, int, int, bool, byte);
[C++] public: Decimal(int, int, int, bool, unsigned char);
[JScript] public function Decimal(int, int, int, Boolean, Byte);

Example

[Visual Basic, C#, C++] 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.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of the Decimal constructor. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
' Example of the Decimal( Integer, Integer, Integer, Boolean, Byte ) 
' constructor.
Imports System
Imports Microsoft.VisualBasic

Module DecimalCtorIIIBByDemo

    ' 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( low As Integer, mid As Integer, _
        high As Integer, isNeg As Boolean, scale as Byte )

        ' Format the constructor for display.
        Dim ctor As String = String.Format( _
            "Decimal( {0}, {1}, {2}, {3}, {4} )", _
            low, mid, high, isNeg, scale )
        Dim valOrExc As String

        ' Construct the Decimal value.
        Try
            Dim decimalNum As New Decimal( _
                low, mid, high, isNeg, scale )

            ' Format and save the Decimal value.
            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 = 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, Integer, " & _
            "Integer, Boolean, Byte ) " & vbCrLf & "constructor " & _
            "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 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( Integer.MaxValue, 0, 0, False, 18 )                
        CreateDecimal( Integer.MaxValue, 0, 0, False, 28 )                
        CreateDecimal( Integer.MaxValue, 0, 0, True, 28 )                
    End Sub 
End Module 

' This example of the Decimal( Integer, Integer, Integer, Boolean, 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

[C#] 
// 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
*/

[C++] 
// Example of the Decimal( int, int, int, bool, unsigned char ) 
// 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 low, int mid, int high, bool isNeg, 
    unsigned char scale )
{
    // Format the constructor for display.
    Object* boxedParams[ ] = __gc new Object*[ 5 ];
        boxedParams[ 0 ] = __box( low );
        boxedParams[ 1 ] = __box( mid );
        boxedParams[ 2 ] = __box( high );
        boxedParams[ 3 ] = __box( isNeg );
        boxedParams[ 4 ] = __box( scale );
    String* ctor = String::Format( 
        S"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( 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, int, " 
        S"int, bool, unsigned char ) \nconstructor " 
        S"generates 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 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
*/

[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.

See Also

Decimal Structure | Decimal Members | System Namespace

Show:
© 2015 Microsoft