This documentation is archived and is not being maintained.

Convert.ToDouble Method (String, IFormatProvider)

Updated: May 2009

Converts the specified string representation of a number to an equivalent double-precision floating-point number, using the specified culture-specific formatting information.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static double ToDouble(
	string value,
	IFormatProvider provider
)

Parameters

value
Type: System.String

A string that contains the number to convert.

provider
Type: System.IFormatProvider

An object that supplies culture-specific formatting information.

Return Value

Type: System.Double
A double-precision floating-point number that is equivalent to the number in value, or 0 (zero) if value is null. Because of differences in precision, the return value may not be exactly equal to value, and for values of value that are less than Double.Epsilon, the return value may also differ depending on processor architecture. For more information, see the Remarks section of System.Double.

ExceptionCondition
FormatException

value is not a number in a valid format.

OverflowException

value represents a number that is less than Double.MinValue or greater than Double.MaxValue.

The return value is the result of invoking the Double.Parse method on value.

provider is an IFormatProvider instance that obtains a NumberFormatInfo object. The NumberFormatInfo object provides culture-specific information about the format of value. If provider is null, the NumberFormatInfo for the current culture is used.

If you prefer not to handle an exception if the conversion fails, you can call the Double.TryParse method instead. It returns a Boolean value that indicates whether the conversion succeeded or failed.

The following example converts string representations of Double values with the ToDouble method, using an IFormatProvider object.

// Example of the Convert.ToDouble( String ) and  
// Convert.ToDouble( String, IFormatProvider ) methods. 
using System;
using System.Globalization;

class ToDoubleProviderDemo
{
    static string formatter = "{0,-22}{1,-20}{2}";

    // Get the exception type name; remove the namespace prefix. 
    static string GetExceptionType( Exception ex )
    {
        string exceptionType = ex.GetType( ).ToString( );
        return exceptionType.Substring( 
            exceptionType.LastIndexOf( '.' ) + 1 );
    }

    static void ConvertToDouble( string numericStr, 
        IFormatProvider provider )
    {
        object defaultValue;
        object providerValue;

        // Convert numericStr to double without a format provider. 
        try
        {
            defaultValue = Convert.ToDouble( numericStr );
        }
        catch( Exception ex )
        {
            defaultValue = GetExceptionType( ex );
        }

        // Convert numericStr to double with a format provider. 
        try
        {
            providerValue = Convert.ToDouble( numericStr, provider );
        }
        catch( Exception ex )
        {
            providerValue = GetExceptionType( ex );
        }

        Console.WriteLine( formatter, numericStr, defaultValue, 
            providerValue );
    }

    static void Main( )
    {
        // Create a NumberFormatInfo object and set several of its 
        // properties that apply to numbers.
        NumberFormatInfo provider = new NumberFormatInfo( );

        provider.NumberDecimalSeparator = ",";
        provider.NumberGroupSeparator = ".";
        provider.NumberGroupSizes = new int[ ] { 3 };

        Console.WriteLine( 
            "This example of\n  Convert.ToDouble( String ) and \n" +
            "  Convert.ToDouble( String, IFormatProvider ) \n" +
            "generates the following output when run in the " +
            "[{0}] culture.", 
            CultureInfo.CurrentCulture.Name );
        Console.WriteLine( "\nSeveral " +
            "strings are converted to double values, using \n" +
            "default formatting and a NumberFormatInfo object.\n");
        Console.WriteLine( formatter, "String to convert", 
            "Default/exception", "Provider/exception" );
        Console.WriteLine( formatter, "-----------------", 
            "-----------------", "------------------" );

        // Convert strings, with and without an IFormatProvider.
        ConvertToDouble( "123456789", provider );
        ConvertToDouble( "12345.6789", provider );
        ConvertToDouble( "12345,6789", provider );
        ConvertToDouble( "123,456.789", provider );
        ConvertToDouble( "123.456,789", provider );
        ConvertToDouble( "123,456,789.0123", provider );
        ConvertToDouble( "123.456.789,0123", provider );
    }
}

/*
This example of
  Convert.ToDouble( String ) and
  Convert.ToDouble( String, IFormatProvider )
generates the following output when run in the [en-US] culture.

Several strings are converted to double values, using
default formatting and a NumberFormatInfo object.

String to convert     Default/exception   Provider/exception
-----------------     -----------------   ------------------
123456789             123456789           123456789
12345.6789            12345.6789          123456789
12345,6789            123456789           12345.6789
123,456.789           123456.789          FormatException
123.456,789           FormatException     123456.789
123,456,789.0123      123456789.0123      FormatException
123.456.789,0123      FormatException     123456789.0123
*/

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

May 2009

Expanded the Remarks section.

Customer feedback.

May 2009

Expanded the Return Value section.

Content bug fix.

Show: