This documentation is archived and is not being maintained.

Convert.ToByte Method (Double)

.NET Framework 1.1

Converts the value of the specified double-precision floating point number to an equivalent 8-bit unsigned integer.

[Visual Basic]
Overloads Public Shared Function ToByte( _
   ByVal value As Double _
) As Byte
[C#]
public static byte ToByte(
 double value
);
[C++]
public: static unsigned char ToByte(
 double value
);
[JScript]
public static function ToByte(
   value : double
) : Byte;

Parameters

value
A double-precision floating point number.

Return Value

value rounded to the nearest 8-bit signed integer. If value is halfway between two whole numbers, the even number is returned; that is, 4.5 is converted to 4, and 5.5 is converted to 6.

Exceptions

Exception Type Condition
OverflowException value is greater than Byte.MaxValue or less than Byte.MinValue.

Example

[Visual Basic, C#, C++] The following code sample illustrates the use of ToByte, converting a Double value to a Byte:

[Visual Basic] 
Public Sub ConvertDoubleByte(ByVal doubleVal As Double)
    Dim byteVal As Byte = 0

    ' Double to Byte conversion can overflow.
    Try
        byteVal = System.Convert.ToByte(doubleVal)
        System.Console.WriteLine("{0} as a Byte is: {1}.", _
            doubleVal, byteVal)
    Catch exception As System.OverflowException
        System.Console.WriteLine( _
            "Overflow in Double-to-Byte conversion.")
    End Try

    ' Byte to Double conversion cannot overflow.
    doubleVal = System.Convert.ToDouble(byteVal)
    System.Console.WriteLine("{0} as a Double is: {1}.", _
                              byteVal, doubleVal)
End Sub

[C#] 
public void ConvertDoubleByte(double doubleVal) {
    byte    byteVal = 0;

    // Double to byte conversion can overflow.
    try {
        byteVal = System.Convert.ToByte(doubleVal);
        System.Console.WriteLine("{0} as a byte is: {1}.",
            doubleVal, byteVal);
    } 
    catch (System.OverflowException) {
        System.Console.WriteLine(
            "Overflow in double-to-byte conversion.");
    }

    // Byte to double conversion cannot overflow.
    doubleVal = System.Convert.ToDouble(byteVal);
    System.Console.WriteLine("{0} as a double is: {1}.",
        byteVal, doubleVal);
}

[C++] 

   void ConvertDoubleByte(double doubleVal)
   {
      Byte byteVal = 0;

      // Double to Byte conversion can overflow.
      try {
         byteVal = System::Convert::ToByte(doubleVal);
         System::Console::WriteLine(S" {0} as a Byte is: {1}.",
            __box(doubleVal), __box(byteVal));
      } catch (System::OverflowException*) {
         System::Console::WriteLine(S"Overflow in double-to-Byte conversion.");
      }

      // Byte to double conversion cannot overflow.
      doubleVal = System::Convert::ToDouble(byteVal);
      System::Console::WriteLine(S" {0} as a double is: {1}.",
         __box(byteVal), __box(doubleVal));
   }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Convert Class | Convert Members | System Namespace | Convert.ToByte Overload List

Show: