次の方法で共有


Convert.ToByte メソッド (Decimal)

指定した Decimal 数の値を等価の 8 ビット符号なし整数に変換します。

Overloads Public Shared Function ToByte( _
   ByVal value As Decimal _) As Byte
[C#]
public static byte ToByte(decimalvalue);
[C++]
public: static unsigned char ToByte(Decimalvalue);
[JScript]
public static function ToByte(
   value : Decimal) : Byte;

パラメータ

戻り値

近似値の 8 ビット符号付き整数に丸められた value。value が 2 つの整数の中間にある場合は、偶数が返されます。たとえば、4.5 は 4 に変換され、5.5 は 6 に変換されます。

例外

例外の種類 条件
OverflowException value が Byte.MaxValue より大きい値か、 Byte.MinValue より小さい値です。

解説

ToByte を使用して、 Decimal の値を Byte に変換する方法については、次のコード例を参照してください。

 
Public Sub ConvertByteDecimal(ByVal byteVal As Byte)
    Dim decimalVal As Decimal

    ' Byte to decimal conversion will not overflow.
    decimalVal = System.Convert.ToDecimal(byteVal)
    System.Console.WriteLine("The byte as a decimal is {0}.", _
                              decimalVal)

    ' Decimal to byte conversion can overflow.
    Try
        byteVal = System.Convert.ToByte(decimalVal)
        System.Console.WriteLine("The Decimal as a byte is {0}.", _
                                  byteVal)
    Catch exception As System.OverflowException
        System.Console.WriteLine( _
            "Overflow in decimal-to-byte conversion.")
    End Try
End Sub

[C#] 
public void ConvertByteDecimal(byte byteVal) {
    decimal decimalVal;

    // Byte to decimal conversion will not overflow.
    decimalVal = System.Convert.ToDecimal(byteVal);
    System.Console.WriteLine("The byte as a decimal is {0}.",
        decimalVal);

    // Decimal to byte conversion can overflow.
    try {
        byteVal = System.Convert.ToByte(decimalVal);
        System.Console.WriteLine("The Decimal as a byte is {0}.",
            byteVal);
    }
    catch (System.OverflowException) {
        System.Console.WriteLine(
            "The decimal value is too large for a byte.");
    }
}

[C++] 

   void ConvertByteDecimal(Byte byteVal)
   {
      Decimal decimalVal;

      // Byte to decimal conversion will not overflow.
      decimalVal = System::Convert::ToDecimal(byteVal);
      System::Console::WriteLine(S"The Byte as a decimal is {0}.",
         __box(decimalVal));

      // Decimal to Byte conversion can overflow.
      try {
         byteVal = System::Convert::ToByte(decimalVal);
         System::Console::WriteLine(S"The Decimal as a Byte is {0}.",
            __box(byteVal));
      } catch (System::OverflowException*) {
         System::Console::WriteLine(S"The decimal value is too large for a Byte.");
      }
   }

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Convert クラス | Convert メンバ | System 名前空間 | Convert.ToByte オーバーロードの一覧