Convert decimal to integer in CSharp

Description

The following code shows how to convert decimal to integer.

Example


using System;//from   w ww  .j  av a2  s . c  o  m
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);
            }
        }
    }
}

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