다음을 통해 공유


Decimal.ToInt16 메서드

지정된 Decimal의 값을 16비트 부호 있는 정수로 변환합니다.

네임스페이스: System
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
Public Shared Function ToInt16 ( _
    value As Decimal _
) As Short
‘사용 방법
Dim value As Decimal
Dim returnValue As Short

returnValue = Decimal.ToInt16(value)
public static short ToInt16 (
    decimal value
)
public:
static short ToInt16 (
    Decimal value
)
public static short ToInt16 (
    Decimal value
)
public static function ToInt16 (
    value : decimal
) : short

매개 변수

반환 값

value에 해당하는 16비트의 부호 있는 정수입니다.

예외

예외 형식 조건

OverflowException

value가 Int16.MinValue보다 작거나 Int16.MaxValue보다 큰 경우

예제

다음 코드 예제에서는 ToInt16 메서드를 사용하여 Decimal 숫자를 Int16 값으로 변환합니다.

' Example of the Decimal.ToInt16 and Decimal.ToUInt16 methods.
Imports System
Imports Microsoft.VisualBasic

Module DecimalToU_Int16Demo

    Dim formatter As String = "{0,16}{1,19}{2,19}"

    ' 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

    ' Convert the Decimal argument; catch exceptions that are thrown.
    Sub DecimalToU_Int16( argument As Decimal )

        Dim Int16Value    As Object
        Dim UInt16Value   As Object

        ' Convert the argument to a Short value.
        Try
            Int16Value = Decimal.ToInt16( argument )
        Catch ex As Exception
            Int16Value = GetExceptionType( ex )
        End Try

        ' Convert the argument to a UInt16 value.
        Try
            UInt16Value = Decimal.ToUInt16( argument )
        Catch ex As Exception
            UInt16Value = GetExceptionType( ex )
        End Try

        Console.WriteLine( formatter, argument, _
            Int16Value, UInt16Value )
    End Sub

    Sub Main( )

        Console.WriteLine( "This example of the " & vbCrLf & _
            "  Decimal.ToInt16( Decimal ) and " & vbCrLf & _
            "  Decimal.ToUInt16( Decimal ) " & vbCrLf & "methods " & _
            "generates the following output. It " & vbCrLf & _
            "displays several converted Decimal values." & vbCrLf )
        Console.WriteLine( formatter, "Decimal argument", _
            "Short/exception", "UInt16/exception" )
        Console.WriteLine( formatter, "----------------", _
            "---------------", "----------------" )

        ' Convert Decimal values and display the results.
        DecimalToU_Int16( 123D )
        DecimalToU_Int16( New Decimal( 123000, 0, 0, False, 3 ) )
        DecimalToU_Int16( 123.999D )
        DecimalToU_Int16( 65535.999D )
        DecimalToU_Int16( 65536D )
        DecimalToU_Int16( 32767.999D )
        DecimalToU_Int16( 32768D )
        DecimalToU_Int16( - 0.999D )
        DecimalToU_Int16( - 1D )
        DecimalToU_Int16( - 32768.999D )
        DecimalToU_Int16( - 32769D )
    End Sub 
End Module 

' 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   UInt16/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.
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.
using namespace System;
#define formatter "{0,16}{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_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 an unsigned short value.
   try
   {
      UInt16Value = Decimal::ToUInt16( argument );
   }
   catch ( Exception^ ex ) 
   {
      UInt16Value = GetExceptionType( ex );
   }

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

int 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", "unsigned short" );
   Console::WriteLine( formatter, "----------------", "---------------", "--------------" );
   
   // Convert Decimal values and display the results.
   DecimalToU_Int16( Decimal::Parse(  "123" ) );
   DecimalToU_Int16( Decimal(123000,0,0,false,3) );
   DecimalToU_Int16( Decimal::Parse(  "123.999" ) );
   DecimalToU_Int16( Decimal::Parse(  "65535.999" ) );
   DecimalToU_Int16( Decimal::Parse(  "65536" ) );
   DecimalToU_Int16( Decimal::Parse(  "32767.999" ) );
   DecimalToU_Int16( Decimal::Parse(  "32768" ) );
   DecimalToU_Int16( Decimal::Parse(  "-0.999" ) );
   DecimalToU_Int16( Decimal::Parse(  "-1" ) );
   DecimalToU_Int16( Decimal::Parse(  "-32768.999" ) );
   DecimalToU_Int16( Decimal::Parse(  "-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     unsigned short
----------------    ---------------     --------------
             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 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

Decimal 구조체
Decimal 멤버
System 네임스페이스
Int16