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.





















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