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
Decimal.ToDouble() Method in C#
The Decimal.ToDouble() method in C# is used to convert the value of the specified Decimal to the equivalent double-precision floating-point number. This conversion is useful when you need to perform operations that require double precision or when interfacing with APIs that expect double values.
Syntax
Following is the syntax −
public static double ToDouble(decimal val);
Parameters
val − The decimal number to convert to a double-precision floating-point number.
Return Value
A double-precision floating-point number that is equivalent to the specified decimal value.
Using Decimal.ToDouble() with Small Values
Let us see an example with small decimal values −
using System;
public class Demo {
public static void Main() {
Decimal val1 = -0.22m;
Decimal val2 = -0.01m;
Console.WriteLine("Decimal 1 = " + val1);
Console.WriteLine("Decimal 2 = " + val2);
Double res1 = Decimal.ToDouble(val1);
Double res2 = Decimal.ToDouble(val2);
Console.WriteLine("Double value1 (Decimal to Double) = " + res1);
Console.WriteLine("Double value2 (Decimal to Double) = " + res2);
}
}
The output of the above code is −
Decimal 1 = -0.22 Decimal 2 = -0.01 Double value1 (Decimal to Double) = -0.22 Double value2 (Decimal to Double) = -0.01
Using Decimal.ToDouble() with Large Values
When dealing with large decimal values, the conversion may result in scientific notation for the double value −
using System;
public class Demo {
public static void Main() {
Decimal val1 = 23828m;
Decimal val2 = 976587687595766665m;
Console.WriteLine("Decimal 1 = " + val1);
Console.WriteLine("Decimal 2 = " + val2);
Double res1 = Decimal.ToDouble(val1);
Double res2 = Decimal.ToDouble(val2);
Console.WriteLine("Double value1 (Decimal to Double) = " + res1);
Console.WriteLine("Double value2 (Decimal to Double) = " + res2);
}
}
The output of the above code is −
Decimal 1 = 23828 Decimal 2 = 976587687595766665 Double value1 (Decimal to Double) = 23828 Double value2 (Decimal to Double) = 9.76587687595767E+17
Precision Considerations
It's important to note that decimal has higher precision than double. The conversion may result in a loss of precision for very precise decimal values −
using System;
public class Demo {
public static void Main() {
Decimal preciseDecimal = 0.123456789012345678901234567890m;
Console.WriteLine("Original Decimal: " + preciseDecimal);
Double convertedDouble = Decimal.ToDouble(preciseDecimal);
Console.WriteLine("Converted Double: " + convertedDouble);
Console.WriteLine("Precision difference visible: " +
(preciseDecimal != (decimal)convertedDouble));
}
}
The output of the above code is −
Original Decimal: 0.1234567890123456789012345679 Converted Double: 0.123456789012346 Precision difference visible: True
Conclusion
The Decimal.ToDouble() method provides a straightforward way to convert decimal values to double-precision floating-point numbers. However, be aware that this conversion may result in precision loss due to the different internal representations of decimal and double types.
