다음을 통해 공유


Decimal.ToByte 메서드

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

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

구문

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

returnValue = Decimal.ToByte(value)
public static byte ToByte (
    decimal value
)
public:
static unsigned char ToByte (
    Decimal value
)
public static byte ToByte (
    Decimal value
)
public static function ToByte (
    value : decimal
) : byte

매개 변수

반환 값

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

예외

예외 형식 조건

OverflowException

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

설명

매개 변수 value는 0에 가장 가까운 정수 값으로 반올림된 다음 결과를 반환합니다.

예제

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

' Example of the Decimal.ToSByte and Decimal.ToByte methods.
Imports System
Imports Microsoft.VisualBasic

Module DecimalToS_ByteDemo

    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 DecimalToS_Byte( argument As Decimal )

        Dim SByteValue    As Object
        Dim ByteValue   As Object

        ' Convert the argument to an SByte value.
        Try
            SByteValue = Decimal.ToSByte( argument )
        Catch ex As Exception
            SByteValue = GetExceptionType( ex )
        End Try

        ' Convert the argument to a Byte value.
        Try
            ByteValue = Decimal.ToByte( argument )
        Catch ex As Exception
            ByteValue = GetExceptionType( ex )
        End Try

        Console.WriteLine( formatter, argument, _
            SByteValue, ByteValue )
    End Sub

    Sub Main( )

        Console.WriteLine( "This example of the " & vbCrLf & _
            "  Decimal.ToSByte( Decimal ) and " & vbCrLf & _
            "  Decimal.ToByte( Decimal ) " & vbCrLf & "methods " & _
            "generates the following output. It " & vbCrLf & _
            "displays several converted Decimal values." & vbCrLf )
        Console.WriteLine( formatter, "Decimal argument", _
            "SByte/exception", "Byte/exception" )
        Console.WriteLine( formatter, "----------------", _
            "---------------", "--------------" )

        ' Convert Decimal values and display the results.
        DecimalToS_Byte( 78D )
        DecimalToS_Byte( New Decimal( 78000, 0, 0, False, 3 ) )
        DecimalToS_Byte( 78.999D )
        DecimalToS_Byte( 255.999D )
        DecimalToS_Byte( 256D )
        DecimalToS_Byte( 127.999D )
        DecimalToS_Byte( 128D )
        DecimalToS_Byte( - 0.999D )
        DecimalToS_Byte( - 1D )
        DecimalToS_Byte( - 128.999D )
        DecimalToS_Byte( - 129D )
    End Sub 
End Module 

' This example of the
'   Decimal.ToSByte( Decimal ) and
'   Decimal.ToByte( Decimal )
' methods generates the following output. It
' displays several converted Decimal values.
' 
' Decimal argument    SByte/exception     Byte/exception
' ----------------    ---------------     --------------
'               78                 78                 78
'           78.000                 78                 78
'           78.999                 78                 78
'          255.999  OverflowException                255
'              256  OverflowException  OverflowException
'          127.999                127                127
'              128  OverflowException                128
'           -0.999                  0                  0
'               -1                 -1  OverflowException
'         -128.999               -128  OverflowException
'             -129  OverflowException  OverflowException
// Example of the decimal.ToSByte and decimal.ToByte methods.
using System;

class DecimalToS_ByteDemo
{
    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 DecimalToS_Byte( decimal argument )
    {
        object SByteValue;
        object ByteValue;

        // Convert the argument to an sbyte value.
        try
        {
            SByteValue = decimal.ToSByte( argument );
        }
        catch( Exception ex )
        {
            SByteValue = GetExceptionType( ex );
        }

        // Convert the argument to a byte value.
        try
        {
            ByteValue = decimal.ToByte( argument );
        }
        catch( Exception ex )
        {
            ByteValue = GetExceptionType( ex );
        }

        Console.WriteLine( formatter, argument, 
            SByteValue, ByteValue );
    }

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

        // Convert decimal values and display the results.
        DecimalToS_Byte( 78M );
        DecimalToS_Byte( new decimal( 78000, 0, 0, false, 3 ) );
        DecimalToS_Byte( 78.999M );
        DecimalToS_Byte( 255.999M );
        DecimalToS_Byte( 256M );
        DecimalToS_Byte( 127.999M );
        DecimalToS_Byte( 128M );
        DecimalToS_Byte( -0.999M );
        DecimalToS_Byte( -1M );
        DecimalToS_Byte( -128.999M );
        DecimalToS_Byte( -129M );
    }
}

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

decimal argument    sbyte/exception     byte/exception
----------------    ---------------     --------------
              78                 78                 78
          78.000                 78                 78
          78.999                 78                 78
         255.999  OverflowException                255
             256  OverflowException  OverflowException
         127.999                127                127
             128  OverflowException                128
          -0.999                  0                  0
              -1                 -1  OverflowException
        -128.999               -128  OverflowException
            -129  OverflowException  OverflowException
*/
// Example of the Decimal::ToByte and Decimal::ToSByte 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 DecimalToS_Byte( Decimal argument )
{
   Object^ ByteValue;
   Object^ SByteValue;
   
   // Convert the argument to an unsigned char value.
   try
   {
      ByteValue = Decimal::ToByte( argument );
   }
   catch ( Exception^ ex ) 
   {
      ByteValue = GetExceptionType( ex );
   }

   
   // Convert the argument to a signed char value.
   try
   {
      SByteValue = Decimal::ToSByte( argument );
   }
   catch ( Exception^ ex ) 
   {
      SByteValue = GetExceptionType( ex );
   }

   Console::WriteLine( formatter, argument, ByteValue, SByteValue );
}

int main()
{
   Console::WriteLine( "This example of the \n"
   "  Decimal::ToByte( Decimal ) and \n"
   "  Decimal::ToSByte( Decimal ) \nmethods "
   "generates the following output. It \ndisplays "
   "several converted Decimal values.\n" );
   Console::WriteLine( formatter, "Decimal argument", "unsigned char", "(signed) char" );
   Console::WriteLine( formatter, "----------------", "-------------", "-------------" );
   
   // Convert Decimal values and display the results.
   DecimalToS_Byte( Decimal::Parse(  "78" ) );
   DecimalToS_Byte( Decimal(78000,0,0,false,3) );
   DecimalToS_Byte( Decimal::Parse(  "78.999" ) );
   DecimalToS_Byte( Decimal::Parse(  "255.999" ) );
   DecimalToS_Byte( Decimal::Parse(  "256" ) );
   DecimalToS_Byte( Decimal::Parse(  "127.999" ) );
   DecimalToS_Byte( Decimal::Parse(  "128" ) );
   DecimalToS_Byte( Decimal::Parse(  "-0.999" ) );
   DecimalToS_Byte( Decimal::Parse(  "-1" ) );
   DecimalToS_Byte( Decimal::Parse(  "-128.999" ) );
   DecimalToS_Byte( Decimal::Parse(  "-129" ) );
}

/*
This example of the
  Decimal::ToByte( Decimal ) and
  Decimal::ToSByte( Decimal )
methods generates the following output. It
displays several converted Decimal values.

Decimal argument      unsigned char      (signed) char
----------------      -------------      -------------
              78                 78                 78
          78.000                 78                 78
          78.999                 78                 78
         255.999                255  OverflowException
             256  OverflowException  OverflowException
         127.999                127                127
             128                128  OverflowException
          -0.999                  0                  0
              -1  OverflowException                 -1
        -128.999  OverflowException               -128
            -129  OverflowException  OverflowException
*/
// Example of the decimal.ToSByte and decimal.ToByte methods.
import System.*;

class DecimalToS_ByteDemo
{
    private final static 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 DecimalToS_Byte(System.Decimal argument)
    {
        Object sbyteValue;
        Object byteValue;
      
        // Convert the argument to an sbyte value.
        try {
            sbyteValue = (System.SByte)System.Decimal.ToSByte(argument);
        }
        catch(System.Exception ex) {
            sbyteValue = GetExceptionType(ex);
        }
          
        // Convert the argument to a byte value.
        try {
            byteValue = (System.Byte)(System.Decimal.ToByte(argument));
        }
        catch(System.Exception ex) {
            byteValue = GetExceptionType(ex);
        }
        Console.WriteLine(formatter, argument, sbyteValue, byteValue);
    }
   
    public static void main(String[] args)
    {
        Console.WriteLine("This example of the \n" 
            + "  decimal.ToSByte( decimal ) and \n" 
            + "  decimal.ToByte( decimal ) \nmethods " 
            + "generates the following output. It \ndisplays " 
            + "several converted decimal values.\n");

        Console.WriteLine(formatter, "decimal argument", "sbyte/exception", 
            "byte/exception");
        Console.WriteLine(formatter, "----------------", "---------------", 
            "--------------");
      
        // Convert decimal values and display the results.
        DecimalToS_Byte(new Decimal(78));
        DecimalToS_Byte(new System.Decimal(78000, 0, 0, false, (ubyte)3));
        DecimalToS_Byte(new System.Decimal(78.999));
        DecimalToS_Byte(new System.Decimal(255.999));
        DecimalToS_Byte(new System.Decimal(256));
        DecimalToS_Byte(new System.Decimal(127.999));
        DecimalToS_Byte(new System.Decimal(128));
        DecimalToS_Byte(new System.Decimal(-0.999));
        DecimalToS_Byte(new System.Decimal(-1));
        DecimalToS_Byte(new System.Decimal(-128.999));
        DecimalToS_Byte(new System.Decimal(-129));
    }
}

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

decimal argument    sbyte/exception     byte/exception
----------------    ---------------     --------------
              78                 78                 78
          78.000                 78                 78
          78.999                 78                 78
         255.999  OverflowException                255
             256  OverflowException  OverflowException
         127.999                127                127
             128  OverflowException                128
          -0.999                  0                  0
              -1                 -1  OverflowException
        -128.999               -128  OverflowException
            -129  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 네임스페이스
Byte 구조체