Decimal - ToByte
Converts the value of the specified to the equivalent 8-bit unsigned integer.
public static void Main() { decimal[] values = { 123m, new Decimal(78000, 0, 0, false, 3), 78.999m, 255.999m, 256m, 127.999m, 128m, -0.999m, -1m, -128.999m, -129m }; foreach (var val in values) { try { // C# Extension Method: Decimal - ToByte byte number = val.ToByte(); Console.WriteLine("{0} --> {1}", val, number); } catch (OverflowException e) { Console.WriteLine("{0}: {1}", e.GetType().Name, val); } } }
View Source
using System; public static partial class Extensions { /// <summary> /// Converts the value of the specified to the equivalent 8-bit unsigned integer. /// </summary> /// <param name="value">The decimal number to convert.</param> /// <returns>An 8-bit unsigned integer equivalent to .</returns> public static Byte ToByte(this Decimal value) { return Decimal.ToByte(value); } }