Decimal - ToInt16
Converts the value of the specified to the equivalent 16-bit signed integer.
static string formatter = "{0,30}{1,23}"; public static void Main() { decimal[] values = { 123m, new Decimal(123000, 0, 0, false, 3), 123.999m, 65535.999m, 65536m, 32767.999m, 32768m, -0.999m, -1m, -32768.999m, -32769m }; Console.WriteLine(formatter, "Decimal value", "Int16"); Console.WriteLine(formatter, "----------------", "------"); foreach (var value in values) { try { // C# Extension Method: Decimal - ToInt16 short number = value.ToInt16(); Console.WriteLine(formatter, value, number); } catch (OverflowException e) { Console.WriteLine(formatter, e.GetType().Name, value); } } }
View Source
using System; public static partial class Extensions { /// <summary> /// Converts the value of the specified to the equivalent 16-bit signed integer. /// </summary> /// <param name="value">The decimal number to convert.</param> /// <returns>A 16-bit signed integer equivalent to .</returns> public static Int16 ToInt16(this Decimal value) { return Decimal.ToInt16(value); } }