Convert.ToDouble Method in C#

The Convert.ToDouble() method in C# converts a specified value to a double-precision floating-point number. This method is part of the Convert class and can handle various data types including integers, strings, booleans, and other numeric types.

Syntax

Following is the syntax for the Convert.ToDouble() method −

public static double ToDouble(object value)
public static double ToDouble(string value)
public static double ToDouble(int value)
public static double ToDouble(long value)
public static double ToDouble(bool value)

Parameters

value − The value to be converted to a double-precision floating-point number. This can be of various types including object, string, int, long, bool, and other numeric types.

Return Value

Returns a double-precision floating-point number equivalent to the specified value, or zero if the value is null.

Using Convert.ToDouble() with Different Data Types

Converting Integers to Double

using System;

public class Demo {
   public static void Main() {
      long[] val = { 340, -200, 1000 };
      double result;
      
      Console.WriteLine("Converting long integers to double:");
      foreach (long number in val) {
         result = Convert.ToDouble(number);
         Console.WriteLine("Converted {0} to {1}", number, result);
      }
   }
}

The output of the above code is −

Converting long integers to double:
Converted 340 to 340
Converted -200 to -200
Converted 1000 to 1000

Converting Strings to Double

using System;

public class Demo {
   public static void Main() {
      string[] values = { "123.45", "-67.89", "0", "999.999" };
      
      Console.WriteLine("Converting strings to double:");
      foreach (string str in values) {
         try {
            double result = Convert.ToDouble(str);
            Console.WriteLine("'{0}' converted to {1}", str, result);
         }
         catch (FormatException) {
            Console.WriteLine("'{0}' is not a valid number", str);
         }
      }
   }
}

The output of the above code is −

Converting strings to double:
'123.45' converted to 123.45
'-67.89' converted to -67.89
'0' converted to 0
'999.999' converted to 999.999

Converting Boolean Values to Double

using System;

public class Demo {
   public static void Main() {
      bool[] boolValues = { true, false };
      
      Console.WriteLine("Converting boolean values to double:");
      foreach (bool b in boolValues) {
         double result = Convert.ToDouble(b);
         Console.WriteLine("{0} converted to {1}", b, result);
      }
   }
}

The output of the above code is −

Converting boolean values to double:
True converted to 1
False converted to 0

Common Use Cases

  • Data Type Conversion: Converting integers, strings, or other numeric types to double for mathematical calculations.

  • User Input Processing: Converting string input from users to double values for numerical operations.

  • Database Operations: Converting database values to double for computation and analysis.

  • API Data Processing: Converting JSON or XML numeric data to double for processing.

Conclusion

The Convert.ToDouble() method provides a reliable way to convert various data types to double-precision floating-point numbers. It handles multiple input types including integers, strings, and booleans, making it versatile for different conversion scenarios in C# applications.

Updated on: 2026-03-17T07:04:35+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements