Convert decimal number to byte in CSharp
Description
The following code shows how to convert decimal number to byte.
Example
//w ww . j a va 2 s .co m
using System;
public class MainClass
{
public static void Main()
{
byte byteVal = 123;
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.
byteVal = System.Convert.ToByte(decimalVal);
System.Console.WriteLine("The Decimal as a byte is {0}.", byteVal);
}
}
The code above generates the following result.