Convert double-precision floating-point number to byte in CSharp

Description

The following code shows how to convert double-precision floating-point number to byte.

Example


using System;// w  w w  .  jav a2 s.c o  m

public class MainClass
{
    public static void Main()
    {
        double doubleVal = 123.123;
        byte byteVal = 0;

        byteVal = System.Convert.ToByte(doubleVal);
        System.Console.WriteLine("{0} as a byte is: {1}.",
            doubleVal, byteVal);

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

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var