Export (0) Print
Expand All

Decimal to Int32 Conversion

.NET Framework 1.1

Converts a Decimal to a 32-bit signed integer.

[Visual Basic]
returnValue = Decimal.op_Explicit(value)
[C#]
public static explicit operator int(
 decimal value
);
[C++]
public: static int op_Explicit();
[JScript]
returnValue = Int32(value);

[Visual Basic] In Visual Basic, you can use the conversion operators defined by a type, but you cannot define your own. You can use the ToInt32 method instead of the Decimal to Int32 conversion.

[JScript] In JScript, you can use the conversion operators defined by a type, but you cannot define your own.

Arguments [Visual Basic, JScript]

value
A Decimal to convert.

Parameters [C#]

value
A Decimal to convert.

Return Value

A 32-bit signed integer that represents the converted Decimal.

Exceptions

Exception Type Condition
OverflowException value is less than Int32.MinValue or greater than Int32.MaxValue.

Example

[C#, C++] The following code example converts Decimal numbers to Int32 values using the explicit Decimal to Int32 conversion.

[C#] 
// Example of the explicit conversions from decimal to int and 
// decimal to uint.
using System;

class DecimalToU_Int32Demo
{
    const string formatter = "{0,17}{1,19}{2,19}";

    // 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 );
    }

    // Convert the decimal argument; catch exceptions that are thrown.
    public static void DecimalToU_Int32( decimal argument )
    {
        object Int32Value;
        object UInt32Value;

        // Convert the argument to an int value.
        try
        {
            Int32Value = (int)argument;
        }
        catch( Exception ex )
        {
            Int32Value = GetExceptionType( ex );
        }

        // Convert the argument to a uint value.
        try
        {
            UInt32Value = (uint)argument;
        }
        catch( Exception ex )
        {
            UInt32Value = GetExceptionType( ex );
        }

        Console.WriteLine( formatter, argument, 
            Int32Value, UInt32Value );
    }

    public static void Main( )
    {
        Console.WriteLine( 
            "This example of the explicit conversions from decimal " +
            "to int \nand decimal to uint generates the following " +
            "output. It displays \nseveral converted decimal " +
            "values.\n" );
        Console.WriteLine( formatter, "decimal argument", 
            "int/exception", "uint/exception" );
        Console.WriteLine( formatter, "----------------", 
            "-------------", "--------------" );

        // Convert decimal values and display the results.
        DecimalToU_Int32( 123M );
        DecimalToU_Int32( new decimal( 123000, 0, 0, false, 3 ) );
        DecimalToU_Int32( 123.999M );
        DecimalToU_Int32( 4294967295.999M );
        DecimalToU_Int32( 4294967296M );
        DecimalToU_Int32( 2147483647.999M );
        DecimalToU_Int32( 2147483648M );
        DecimalToU_Int32( - 0.999M );
        DecimalToU_Int32( - 1M );
        DecimalToU_Int32( - 2147483648.999M );
        DecimalToU_Int32( - 2147483649M );
    }
}

/*
This example of the explicit conversions from decimal to int
and decimal to uint generates the following output. It displays
several converted decimal values.

 decimal argument      int/exception     uint/exception
 ----------------      -------------     --------------
              123                123                123
          123.000                123                123
          123.999                123                123
   4294967295.999  OverflowException         4294967295
       4294967296  OverflowException  OverflowException
   2147483647.999         2147483647         2147483647
       2147483648  OverflowException         2147483648
           -0.999                  0                  0
               -1                 -1  OverflowException
  -2147483648.999        -2147483648  OverflowException
      -2147483649  OverflowException  OverflowException
*/

[C++] 
// Example of the explicit conversions from Decimal to int and 
// Decimal to unsigned int.
#using <mscorlib.dll>
using namespace System;

const __wchar_t* formatter = L"{0,17}{1,19}{2,19}";

// Get the exception type name; remove the namespace prefix.
String* GetExceptionType( Exception* ex )
{
    String* exceptionType = ex->GetType( )->ToString( );
    return exceptionType->Substring( 
        exceptionType->LastIndexOf( '.' ) + 1 );
}

// Convert the Decimal argument; catch exceptions that are thrown.
void DecimalToU_Int32( Decimal argument )
{
    Object* Int32Value;
    Object* UInt32Value;

    // Convert the argument to an int value.
    try
    {
        Int32Value = __box( (int)argument );
    }
    catch( Exception* ex )
    {
        Int32Value = GetExceptionType( ex );
    }

    // Convert the argument to an unsigned int value.
    try
    {
        UInt32Value = __box( (unsigned int)argument );
    }
    catch( Exception* ex )
    {
        UInt32Value = GetExceptionType( ex );
    }

    Console::WriteLine( formatter, __box( argument ), 
        Int32Value, UInt32Value );
}

void main( )
{
    Console::WriteLine( 
        S"This example of the explicit conversions from Decimal to " 
        S"int \nand Decimal to unsigned int generates the " 
        S"following output. \nIt displays several converted Decimal " 
        S"values.\n" );
    Console::WriteLine( formatter, S"Decimal argument", 
        S"int", S"unsigned int" );
    Console::WriteLine( formatter, S"----------------", 
        S"---", S"------------" );

    // Convert Decimal values and display the results.
    DecimalToU_Int32( Decimal::Parse( "123" ) );
    DecimalToU_Int32( Decimal( 123000, 0, 0, false, 3 ) );
    DecimalToU_Int32( Decimal::Parse( "123.999" ) );
    DecimalToU_Int32( Decimal::Parse( "4294967295.999" ) );
    DecimalToU_Int32( Decimal::Parse( "4294967296" ) );
    DecimalToU_Int32( Decimal::Parse( "2147483647.999" ) );
    DecimalToU_Int32( Decimal::Parse( "2147483648" ) );
    DecimalToU_Int32( Decimal::Parse( "-0.999" ) );
    DecimalToU_Int32( Decimal::Parse( "-1" ) );
    DecimalToU_Int32( Decimal::Parse( "-2147483648.999" ) );
    DecimalToU_Int32( Decimal::Parse( "-2147483649" ) );
}

/*
This example of the explicit conversions from Decimal to int
and Decimal to unsigned int generates the following output.
It displays several converted Decimal values.

 Decimal argument                int       unsigned int
 ----------------                ---       ------------
              123                123                123
          123.000                123                123
          123.999                123                123
   4294967295.999  OverflowException         4294967295
       4294967296  OverflowException  OverflowException
   2147483647.999         2147483647         2147483647
       2147483648  OverflowException         2147483648
           -0.999                  0                  0
               -1                 -1  OverflowException
  -2147483648.999        -2147483648  OverflowException
      -2147483649  OverflowException  OverflowException
*/

[Visual Basic, JScript] No example is available for Visual Basic or JScript. To view a 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 | Int32

Show:
© 2014 Microsoft