Mega Code Archive

 
Categories / C# / Data Types
 

Convert decimal to integer

using System; using System.Globalization; public class Example {     public static void Main()     {         decimal[] values = { Decimal.MinValue, -10.23m, -12m, 0m, 147m,                           1.55m, 92.16m, Decimal.MaxValue };         int result;         foreach (decimal value in values)         {             try             {                 result = Convert.ToInt32(value);                 Console.WriteLine(result);             }             catch (OverflowException)             {                 Console.WriteLine("{0} is outside the range of the Int32 type.",                                   value);             }         }     } }