Export (0) Print
Expand All

Decimal Constructor (Int32)

.NET Framework 1.1

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

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

Parameters

value
The value to represent as a Decimal.

Example

[Visual Basic, C#, C++] The following code example creates several Decimal numbers using the constructor overload that initializes a Decimal structure with an Int32 value.

[Visual Basic] 
' Example of the Decimal( Integer ) constructor.
Imports System
Imports Microsoft.VisualBasic

Module DecimalCtorIDemo

    ' Create a Decimal object and display its value.
    Sub CreateDecimal( value As Integer, valToStr As String )

        Dim decimalNum As New Decimal( value )

        ' Format the constructor for display.
        Dim ctor As String = _
            String.Format( "Decimal( {0} )", valToStr )

        ' Display the constructor and its value.
        Console.WriteLine( "{0,-33}{1,16}", ctor, decimalNum )
    End Sub
    
    Sub Main( )

        Console.WriteLine( _
            "This example of the Decimal( Integer ) constructor " & _
            vbCrLf & "generates the following output." & vbCrLf )
        Console.WriteLine( "{0,-33}{1,16}", "Constructor", "Value" )
        Console.WriteLine( "{0,-33}{1,16}", "-----------", "-----" )

        ' Construct Decimal objects from Integer values.
        CreateDecimal( Integer.MinValue, "Integer.MinValue" )                
        CreateDecimal( Integer.MaxValue, "Integer.MaxValue" )                
        CreateDecimal( 0, "0" )                
        CreateDecimal( 999999999, "999999999" )                
        CreateDecimal( &H40000000, "&H40000000" )                
        CreateDecimal( &HC0000000, "&HC0000000" )                
    End Sub 
End Module 

' This example of the Decimal( Integer ) constructor
' generates the following output.
' 
' Constructor                                 Value
' -----------                                 -----
' Decimal( Integer.MinValue )           -2147483648
' Decimal( Integer.MaxValue )            2147483647
' Decimal( 0 )                                    0
' Decimal( 999999999 )                    999999999
' Decimal( &H40000000 )                  1073741824
' Decimal( &HC0000000 )                 -1073741824

[C#] 
// Example of the decimal( int ) constructor.
using System;

class DecimalCtorIDemo
{
    // Create a decimal object and display its value.
    public static void CreateDecimal( int value, string valToStr )
    {
        decimal decimalNum = new decimal( value );

        // Format the constructor for display.
        string ctor = String.Format( "decimal( {0} )", valToStr );

        // Display the constructor and its value.
        Console.WriteLine( "{0,-30}{1,16}", ctor, decimalNum );
    }
    
    public static void Main( )
    {
        Console.WriteLine( "This example of the decimal( int ) " +
            "constructor \ngenerates the following output.\n" );
        Console.WriteLine( "{0,-30}{1,16}", "Constructor", "Value" );
        Console.WriteLine( "{0,-30}{1,16}", "-----------", "-----" );

        // Construct decimal objects from int values.
        CreateDecimal( int.MinValue, "int.MinValue" );
        CreateDecimal( int.MaxValue, "int.MaxValue" );
        CreateDecimal( 0, "0" );
        CreateDecimal( 999999999, "999999999" );
        CreateDecimal( 0x40000000, "0x40000000" );
        CreateDecimal( unchecked( (int)0xC0000000 ), 
            "(int)0xC0000000" );
    }
}

/*
This example of the decimal( int ) constructor
generates the following output.

Constructor                              Value
-----------                              -----
decimal( int.MinValue )            -2147483648
decimal( int.MaxValue )             2147483647
decimal( 0 )                                 0
decimal( 999999999 )                 999999999
decimal( 0x40000000 )               1073741824
decimal( (int)0xC0000000 )         -1073741824
*/

[C++] 
// Example of the Decimal( int ) constructor.
#using <mscorlib.dll>
using namespace System;

// Create a Decimal object and display its value.
void CreateDecimal( int value, String* valToStr )
{
    Decimal decimalNum = Decimal( value );

    // Format the constructor for display.
    String* ctor = String::Format( S"Decimal( {0} )", valToStr );

    // Display the constructor and its value.
    Console::WriteLine( S"{0,-30}{1,16}", ctor, __box( decimalNum ) );
}

void main( )
{
    Console::WriteLine( S"This example of the Decimal( int ) " 
        S"constructor \ngenerates the following output.\n" );
    Console::WriteLine( S"{0,-30}{1,16}", S"Constructor", S"Value" );
    Console::WriteLine( S"{0,-30}{1,16}", S"-----------", S"-----" );

    // Construct Decimal objects from int values.
    CreateDecimal( Int32::MinValue, S"Int32::MinValue" );
    CreateDecimal( Int32::MaxValue, S"Int32::MaxValue" );
    CreateDecimal( 0, S"0" );
    CreateDecimal( 999999999, S"999999999" );
    CreateDecimal( 0x40000000, S"0x40000000" );
    CreateDecimal( (int)0xC0000000, S"(int)0xC0000000" );
}

/*
This example of the Decimal( int ) constructor
generates the following output.

Constructor                              Value
-----------                              -----
Decimal( Int32::MinValue )         -2147483648
Decimal( Int32::MaxValue )          2147483647
Decimal( 0 )                                 0
Decimal( 999999999 )                 999999999
Decimal( 0x40000000 )               1073741824
Decimal( (int)0xC0000000 )         -1073741824
*/

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

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Decimal Structure | Decimal Members | System Namespace | Decimal Constructor Overload List

Show:
© 2014 Microsoft