Decimal - ToInt32
Converts the value of the specified to the equivalent 32-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, 4294967295.999m, 4294967296m, 4294967296m, 2147483647.999m, 2147483648m, -0.999m, -1m, -2147483648.999m, -2147483649m }; Console.WriteLine(formatter, "Decimal value", "Int32"); Console.WriteLine(formatter, "----------------", "------"); foreach (var value in values) { try { // C# Extension Method: Decimal - ToInt32 int number = value.ToInt32(); 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 32-bit signed integer. /// </summary> /// <param name="d">The decimal number to convert.</param> /// <returns>A 32-bit signed integer equivalent to the value of .</returns> public static Int32 ToInt32(this Decimal d) { return Decimal.ToInt32(d); } }