Este artigo foi traduzido por máquina. Para visualizar o arquivo em inglês, marque a caixa de seleção Inglês. Você também pode exibir o texto Em inglês em uma janela pop-up, movendo o ponteiro do mouse sobre o texto.
Tradução
Inglês

Decimal.ToInt32 Método

Converts the value of the specified Decimal to the equivalent 32-bit signed integer.

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

public static int ToInt32(
	decimal d
)

Parâmetros

d
Tipo: System.Decimal
The Decimal value to convert.

Valor de retorno

Tipo: System.Int32
A 32-bit signed integer equivalent to the value of d.

ExceçãoCondição
OverflowException

d is less than Int32.MinValue or greater than Int32.MaxValue.

O valor de retorno é a parte integral do valor decimal; dígitos fracionários são truncados.

The following code example converts Decimal numbers to Int32 values using the ToInt32 method.

// Example of the decimal.ToInt32 and decimal.ToUInt32 methods.
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 = decimal.ToInt32( argument );
        }
        catch( Exception ex )
        {
            Int32Value = GetExceptionType( ex );
        }

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

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

    public static void Main( )
    {
        Console.WriteLine( "This example of the \n" +
            "  decimal.ToInt32( decimal ) and \n" +
            "  decimal.ToUInt32( decimal ) \nmethods " +
            "generates the following output. It \ndisplays " +
            "several 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
  decimal.ToInt32( decimal ) and
  decimal.ToUInt32( decimal )
methods 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
*/


// Example of the decimal.ToInt32 and decimal.ToUInt32 methods.
import System.*;

class DecimalToU_Int32Demo
{
    private static final String formatter = "{0,17}{1,19}{2,19}";

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

    // Convert the decimal argument; catch exceptions that are thrown.
    public static void DecimalToU_Int32(System.Decimal argument) 
    {
        Object int32Value;
        Object uint32Value;

        // Convert the argument to an int value.
        try {
            int32Value = (System.Int32)System.Decimal.ToInt32(argument);
        }
        catch(System.Exception  ex) {
            int32Value = GetExceptionType(ex);
        }

        // Convert the argument to a uint value.
        try {
            uint32Value = System.Decimal.ToUInt32(argument);
        }
        catch(System.Exception  ex) {
            uint32Value = GetExceptionType(ex);
        }
        Console.WriteLine(formatter, argument, int32Value, uint32Value);
    }

    public static void main(String[] args1)
    {
        Console.WriteLine("This example of the \n" 
            + "  decimal.ToInt32( decimal ) and \n" 
            + "  decimal.ToUInt32( decimal ) \nmethods " 
            + "generates the following output. It \ndisplays " 
            + "several 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(new System.Decimal(123));
        DecimalToU_Int32(new System.Decimal(123000, 0, 0, false, (ubyte)3));
        DecimalToU_Int32(new System.Decimal(123.999));
        DecimalToU_Int32(new System.Decimal(4294967295.999));
        DecimalToU_Int32(new System.Decimal(4294967296F));
        DecimalToU_Int32(new System.Decimal(2147483647.999));
        DecimalToU_Int32(new System.Decimal(2147483648F));
        DecimalToU_Int32(new System.Decimal(-0.999));
        DecimalToU_Int32(new System.Decimal(-1));
        DecimalToU_Int32(new System.Decimal(-2147483648.999));
        DecimalToU_Int32(new System.Decimal(-2147483649F));
   }
}

/*
This example of the
  decimal.ToInt32( decimal ) and
  decimal.ToUInt32( decimal )
methods 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
       4294967000  OverflowException         4294967000
   2147483647.999         2147483647         2147483647
       2147484000  OverflowException         2147484000
           -0.999                  0                  0
               -1                 -1  OverflowException
  -2147483648.999        -2147483648  OverflowException
      -2147484000  OverflowException  OverflowException
*/


Contribuições da comunidade

ADICIONAR
Mostrar: