Convert double to integer and catch OverflowException in CSharp

Description

The following code shows how to convert double to integer and catch OverflowException.

Example


using System;//from  www  . j  a  v  a  2  s.  c  om
using System.Globalization;

public class Example
{
    public static void Main()
    {
        double dbl2 = 999111111188811.999D;
        try
        {
            int number2 = (int)dbl2;
            Console.WriteLine(number2);
        }
        catch (OverflowException)
        {
            Console.WriteLine("{0} is out of range of an Int32.", dbl2);
        }

    }
}

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