Convert.ToDouble(String, IFormatProvider) Method in C#

The Convert.ToDouble(String, IFormatProvider) method in C# converts a string representation of a number to an equivalent double-precision floating-point number using specified culture-specific formatting information. This method is particularly useful when working with numeric strings that follow different regional formatting conventions.

Syntax

Following is the syntax −

public static double ToDouble(string value, IFormatProvider provider);

Parameters

  • value − A string that contains the number to convert.

  • provider − An object that supplies culture-specific formatting information. If null, the current culture is used.

Return Value

Returns a double-precision floating-point number equivalent to the numeric value or symbol specified in value.

Using Custom Number Format Provider

You can create a custom NumberFormatInfo to define specific decimal and group separators −

using System;
using System.Globalization;

public class Demo {
    public static void Main() {
        string val = "876876, 878";
        NumberFormatInfo formatProvider = new NumberFormatInfo();
        formatProvider.NumberDecimalSeparator = ", ";
        formatProvider.NumberGroupSeparator = ".";
        formatProvider.NumberGroupSizes = new int[] { 2 };
        
        Console.WriteLine("Original string: " + val);
        Console.WriteLine("Converted double value:");
        double res = Convert.ToDouble(val, formatProvider);
        Console.WriteLine("{0}", res);
    }
}

The output of the above code is −

Original string: 876876, 878
Converted double value:
876876.878

Using Different Culture Providers

Different cultures use different decimal separators. Here's how to handle various regional formats −

using System;
using System.Globalization;

public class CultureDemo {
    public static void Main() {
        string germanNumber = "1234,56";
        string usNumber = "1234.56";
        
        // German culture uses comma as decimal separator
        CultureInfo germanCulture = new CultureInfo("de-DE");
        double germanResult = Convert.ToDouble(germanNumber, germanCulture);
        
        // US culture uses dot as decimal separator
        CultureInfo usCulture = new CultureInfo("en-US");
        double usResult = Convert.ToDouble(usNumber, usCulture);
        
        Console.WriteLine("German format '1234,56' converted: " + germanResult);
        Console.WriteLine("US format '1234.56' converted: " + usResult);
        
        // Using invariant culture
        double invariantResult = Convert.ToDouble("9876.543", CultureInfo.InvariantCulture);
        Console.WriteLine("Invariant culture '9876.543' converted: " + invariantResult);
    }
}

The output of the above code is −

German format '1234,56' converted: 1234.56
US format '1234.56' converted: 1234.56
Invariant culture '9876.543' converted: 9876.543

Exception Handling

The method throws exceptions for invalid input. Here's how to handle them properly −

using System;
using System.Globalization;

public class ExceptionDemo {
    public static void Main() {
        string[] testValues = { "123.45", "invalid", null, "?", "-?" };
        
        foreach (string value in testValues) {
            try {
                double result = Convert.ToDouble(value, CultureInfo.InvariantCulture);
                Console.WriteLine("'{0}' converted to: {1}", value ?? "null", result);
            }
            catch (ArgumentNullException) {
                Console.WriteLine("'{0}' - ArgumentNullException: Value cannot be null", value ?? "null");
            }
            catch (FormatException) {
                Console.WriteLine("'{0}' - FormatException: Invalid format", value);
            }
            catch (OverflowException) {
                Console.WriteLine("'{0}' - OverflowException: Value too large", value);
            }
        }
    }
}

The output of the above code is −

'123.45' converted to: 123.45
'invalid' - FormatException: Invalid format
'null' - ArgumentNullException: Value cannot be null
'?' converted to: ?
'-?' converted to: -?

Conclusion

The Convert.ToDouble(String, IFormatProvider) method provides flexible string-to-double conversion with culture-specific formatting support. It's essential for applications that process numeric data from different regional sources or require custom number formatting rules.

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

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements