Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to convert a Double value to an Int64 value
To convert a Double value to an Int64 value, use the Convert.ToInt64() method. This method performs a rounded conversion from a 64-bit floating-point number to a 64-bit signed integer.
Int64 represents a 64-bit signed integer and is equivalent to the long data type in C#. The conversion truncates the decimal portion and rounds to the nearest integer value.
Syntax
Following is the syntax for converting a double to Int64 −
long result = Convert.ToInt64(doubleValue);
Parameters
doubleValue − A double-precision floating-point number to be converted.
Return Value
Returns a 64-bit signed integer that is equivalent to the rounded value of the double parameter.
Using Convert.ToInt64() with Scientific Notation
The method works seamlessly with scientific notation values. Let's convert a large double value expressed in scientific notation −
using System;
public class Demo {
public static void Main() {
double val = 23.951213e12;
long longVal = Convert.ToInt64(val);
Console.WriteLine("Original double: {0:E}", val);
Console.WriteLine("Converted Int64: {0:N0}", longVal);
}
}
The output of the above code is −
Original double: 2.395121E+012 Converted Int64: 23,951,213,000,000
Using Convert.ToInt64() with Decimal Values
When converting doubles with decimal portions, the method performs rounding to the nearest integer −
using System;
public class Demo {
public static void Main() {
double val1 = 123.45;
double val2 = 123.67;
double val3 = -456.23;
long longVal1 = Convert.ToInt64(val1);
long longVal2 = Convert.ToInt64(val2);
long longVal3 = Convert.ToInt64(val3);
Console.WriteLine("{0} converts to {1}", val1, longVal1);
Console.WriteLine("{0} converts to {1}", val2, longVal2);
Console.WriteLine("{0} converts to {1}", val3, longVal3);
}
}
The output of the above code is −
123.45 converts to 123 123.67 converts to 124 -456.23 converts to -456
Using Convert.ToInt64() with Range Validation
It's important to handle cases where the double value might exceed the Int64 range to avoid overflow exceptions −
using System;
public class Demo {
public static void Main() {
double[] values = { 9223372036854775807.0, -9223372036854775808.0, 123.456 };
foreach (double val in values) {
try {
long result = Convert.ToInt64(val);
Console.WriteLine("Double {0} ? Int64 {1}", val, result);
} catch (OverflowException) {
Console.WriteLine("Double {0} is outside Int64 range", val);
}
}
}
}
The output of the above code is −
Double 9.223372036854776E+18 ? Int64 9223372036854775807 Double -9.223372036854776E+18 ? Int64 -9223372036854775808 Double 123.456 ? Int64 123
Conclusion
The Convert.ToInt64() method provides a reliable way to convert double values to 64-bit signed integers with automatic rounding. Always consider potential overflow exceptions when working with very large double values that may exceed the Int64 range.
