다음을 통해 공유


Decimal.ToInt32 메서드

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

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

구문

‘선언
Public Shared Function ToInt32 ( _
    d As Decimal _
) As Integer
‘사용 방법
Dim d As Decimal
Dim returnValue As Integer

returnValue = Decimal.ToInt32(d)
public static int ToInt32 (
    decimal d
)
public:
static int ToInt32 (
    Decimal d
)
public static int ToInt32 (
    Decimal d
)
public static function ToInt32 (
    d : decimal
) : int

매개 변수

  • d
    변환할 Decimal 값입니다.

반환 값

d의 값에 해당하는 32비트의 부호 있는 정수입니다.

예외

예외 형식 조건

OverflowException

d가 Int32.MinValue보다 작거나 Int32.MaxValue보다 큰 경우

설명

반환 값은 10진 값의 정수 부분입니다. 소수 자리는 잘립니다.

예제

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

' Example of the Decimal.ToInt32 and Decimal.ToUInt32 methods.
Imports System
Imports Microsoft.VisualBasic

Module DecimalToU_Int32Demo

    Dim formatter As String = "{0,17}{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_Int32( argument As Decimal )

        Dim Int32Value    As Object
        Dim UInt32Value   As Object

        ' Convert the argument to an Integer value.
        Try
            Int32Value = Decimal.ToInt32( argument )
        Catch ex As Exception
            Int32Value = GetExceptionType( ex )
        End Try

        ' Convert the argument to a UInt32 value.
        Try
            UInt32Value = Decimal.ToUInt32( argument )
        Catch ex As Exception
            UInt32Value = GetExceptionType( ex )
        End Try

        Console.WriteLine( formatter, argument, _
            Int32Value, UInt32Value )
    End Sub

    Sub Main( )

        Console.WriteLine( "This example of the " & vbCrLf & _
            "  Decimal.ToInt32( Decimal ) and " & vbCrLf & _
            "  Decimal.ToUInt32( Decimal ) " & vbCrLf & "methods " & _
            "generates the following output. It " & vbCrLf & _
            "displays several converted Decimal values." & vbCrLf )
        Console.WriteLine( formatter, "Decimal argument", _
            "Integer/exception", "UInt32/exception" )
        Console.WriteLine( formatter, "----------------", _
            "-----------------", "----------------" )

        ' Convert Decimal values and display the results.
        DecimalToU_Int32( 123D )
        DecimalToU_Int32( New Decimal( 123000, 0, 0, False, 3 ) )
        DecimalToU_Int32( 123.999D )
        DecimalToU_Int32( 4294967295.999D )
        DecimalToU_Int32( 4294967296D )
        DecimalToU_Int32( 2147483647.999D )
        DecimalToU_Int32( 2147483648D )
        DecimalToU_Int32( - 0.999D )
        DecimalToU_Int32( - 1D )
        DecimalToU_Int32( - 2147483648.999D )
        DecimalToU_Int32( - 2147483649D )
    End Sub 
End Module 

' This example of the
'   Decimal.ToInt32( Decimal ) and
'   Decimal.ToUInt32( Decimal )
' methods generates the following output. It
' displays several converted Decimal values.
' 
'  Decimal argument  Integer/exception   UInt32/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.
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.
using namespace System;
#define formatter "{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 = Decimal::ToInt32( argument );
   }
   catch ( Exception^ ex ) 
   {
      Int32Value = GetExceptionType( ex );
   }

   
   // Convert the argument to an unsigned int value.
   try
   {
      UInt32Value = Decimal::ToUInt32( argument );
   }
   catch ( Exception^ ex ) 
   {
      UInt32Value = GetExceptionType( ex );
   }

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

int 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", "unsigned int" );
   Console::WriteLine( formatter, "----------------", "-------------", "------------" );
   
   // 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
  Decimal::ToInt32( Decimal ) and
  Decimal::ToUInt32( Decimal )
methods generates the following output. It
displays several converted Decimal values.

 Decimal argument      int/exception       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
*/
// 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
*/

플랫폼

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 네임스페이스
Int32