Metodo ToInt16

Metodo Decimal.ToInt16

Aggiornamento: novembre 2007

Converte il valore dell'oggetto Decimal specificato nel numero integer con segno a 16 bit equivalente.

Spazio dei nomi:  System
Assembly:  mscorlib (in mscorlib.dll)

public static short ToInt16(
	decimal value
)
public static short ToInt16(
	Decimal value
)
public static function ToInt16(
	value : decimal
) : short

Parametri

value
Tipo: System.Decimal
Valore Decimal.

Valore restituito

Tipo: System.Int16
Integer con segno a 16 bit equivalente a value.

EccezioneCondizione
OverflowException

value è minore di Int16.MinValue o maggiore di Int16.MaxValue.

Nell'esempio di codice riportato di seguito vengono convertiti i numeri Decimal in valori Int16 mediante il metodo ToInt16.

// Example of the decimal.ToInt16 and decimal.ToUInt16 methods.
using System;

class DecimalToU_Int16Demo
{
    const string formatter = "{0,16}{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_Int16( decimal argument )
    {
        object Int16Value;
        object UInt16Value;

        // Convert the argument to a short value.
        try
        {
            Int16Value = decimal.ToInt16( argument );
        }
        catch( Exception ex )
        {
            Int16Value = GetExceptionType( ex );
        }

        // Convert the argument to a ushort value.
        try
        {
            UInt16Value = decimal.ToUInt16( argument );
        }
        catch( Exception ex )
        {
            UInt16Value = GetExceptionType( ex );
        }

        Console.WriteLine( formatter, argument, 
            Int16Value, UInt16Value );
    }

    public static void Main( )
    {
        Console.WriteLine( "This example of the \n" +
            "  decimal.ToInt16( decimal ) and \n" +
            "  decimal.ToUInt16( decimal ) \nmethods " +
            "generates the following output. It \ndisplays " +
            "several converted decimal values.\n" );
        Console.WriteLine( formatter, "decimal argument", 
            "short/exception", "ushort/exception" );
        Console.WriteLine( formatter, "----------------", 
            "---------------", "----------------" );

        // Convert decimal values and display the results.
        DecimalToU_Int16( 123M );
        DecimalToU_Int16( new decimal( 123000, 0, 0, false, 3 ) );
        DecimalToU_Int16( 123.999M );
        DecimalToU_Int16( 65535.999M );
        DecimalToU_Int16( 65536M );
        DecimalToU_Int16( 32767.999M );
        DecimalToU_Int16( 32768M );
        DecimalToU_Int16( - 0.999M );
        DecimalToU_Int16( - 1M );
        DecimalToU_Int16( - 32768.999M );
        DecimalToU_Int16( - 32769M );
    }
}

/*
This example of the
  decimal.ToInt16( decimal ) and
  decimal.ToUInt16( decimal )
methods generates the following output. It
displays several converted decimal values.

decimal argument    short/exception   ushort/exception
----------------    ---------------   ----------------
             123                123                123
         123.000                123                123
         123.999                123                123
       65535.999  OverflowException              65535
           65536  OverflowException  OverflowException
       32767.999              32767              32767
           32768  OverflowException              32768
          -0.999                  0                  0
              -1                 -1  OverflowException
      -32768.999             -32768  OverflowException
          -32769  OverflowException  OverflowException
*/


// Example of the decimal.ToInt16 and decimal.ToUInt16 methods.
import System.* ;

class DecimalToU_Int16Demo
{
    private static final String formatter = "{0,16}{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_Int16(System.Decimal argument) 
    {
        Object int16Value;
        Object uint16Value;

        // Convert the argument to a short value.
        try {
            int16Value = (System.Int16)System.Decimal.ToInt16(argument);
        }
        catch(System.Exception ex){

            int16Value = GetExceptionType(ex);
        }

        // Convert the argument to a ushort value.
        try {
            uint16Value = System.Decimal.ToUInt16(argument);
        }
        catch(System.Exception ex){
            uint16Value = GetExceptionType(ex);
        }
        Console.WriteLine(formatter, argument, int16Value, uint16Value);
    }

    public static void main(String[] args)
    {
        Console.WriteLine("This example of the \n" 
            + "  decimal.ToInt16( decimal ) and \n" 
            + "  decimal.ToUInt16( decimal ) \nmethods " 
            + "generates the following output. It \ndisplays " 
            + "several converted decimal values.\n");

        Console.WriteLine(formatter, "decimal argument", 
            "short/exception", "ushort/exception");

        Console.WriteLine(formatter, "----------------", 
            "---------------", "----------------");

        // Convert decimal values and display the results.
        DecimalToU_Int16(new System.Decimal(123));
        DecimalToU_Int16(new System.Decimal(123000, 0, 0, false, (ubyte)3));
        DecimalToU_Int16(new System.Decimal(123.999));
        DecimalToU_Int16(new System.Decimal(65535.999));
        DecimalToU_Int16(new System.Decimal(65536));
        DecimalToU_Int16(new System.Decimal(32767.999));
        DecimalToU_Int16(new System.Decimal(32768));
        DecimalToU_Int16(new System.Decimal(-0.999));
        DecimalToU_Int16(new System.Decimal(-1));
        DecimalToU_Int16(new System.Decimal(-32768.999));
        DecimalToU_Int16(new System.Decimal(-32769));
    }
}

/*
This example of the
  decimal.ToInt16( decimal ) and
  decimal.ToUInt16( decimal )
methods generates the following output. It
displays several converted decimal values.

decimal argument    short/exception   ushort/exception
----------------    ---------------   ----------------
             123                123                123
         123.000                123                123
         123.999                123                123
       65535.999  OverflowException              65535
           65536  OverflowException  OverflowException
       32767.999              32767              32767
           32768  OverflowException              32768
          -0.999                  0                  0
              -1                 -1  OverflowException
      -32768.999             -32768  OverflowException
          -32769  OverflowException  OverflowException
*/


Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition , Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile per Smartphone, Windows Mobile per Pocket PC, Xbox 360

.NET Framework e .NET Compact Framework non supportano tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema di .NET Framework.

.NET Framework

Supportato in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supportato in: 3.5, 2.0, 1.0

XNA Framework

Supportato in: 2.0, 1.0

Aggiunte alla community

AGGIUNGI
Mostra:
© 2016 Microsoft