Export (0) Print
Expand All

Convert.ToDouble Method (String, IFormatProvider)

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
)
public static double ToDouble (
	String value, 
	IFormatProvider provider
)
public static function ToDouble (
	value : String, 
	provider : IFormatProvider
) : double
Not applicable.

Parameters

value

A String containing a number to convert.

provider

An IFormatProvider interface implementation that supplies culture-specific formatting information.

Return Value

A double-precision floating point number equivalent to the value of value. -or- Zero if value is a null reference (Nothing in Visual Basic).

Exception typeCondition

FormatException

value is not a number in a valid format.

OverflowException

value represents a number less than MinValue or greater than 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 a null reference (Nothing in Visual Basic), the NumberFormatInfo for the current culture is used.

The following code 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
*/

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

class ToDoubleProviderDemo
{
    private static String formatter = "{0,-22}{1,-20}{2}";
    // Get the exception type name; remove the namespace prefix.
    static String GetExceptionType(System.Exception ex)
    {
        String exceptionType = ex.GetType().ToString();
        return exceptionType.Substring((exceptionType.LastIndexOf('.') + 1)) ;
    } //GetExceptionType   
   
    static void ConvertToDouble(String numericStr, IFormatProvider provider)
    {
        Object defaultValue;
        Object providerValue;
      
        // Convert numericStr to double without a format provider.
        try {
            defaultValue = System.Convert.ToString(
                (Convert.ToDouble(numericStr)));
        }
        catch(System.Exception  ex) {
            defaultValue = GetExceptionType(ex);
        }
      
        // Convert numericStr to double with a format provider.
        try {
            providerValue =    System.Convert.ToString(
                Convert.ToDouble(numericStr, provider));
        }
        catch(System.Exception  ex) {      
            providerValue = GetExceptionType(ex);
        }
      
        Console.WriteLine(formatter, numericStr, defaultValue, providerValue);
    } //ConvertToDouble   
   
    public static void main(String[] args)
    {    
        // Create a NumberFormatInfo object and set several of its
        // properties that apply to numbers.
        NumberFormatInfo provider =  new NumberFormatInfo();
      
        provider.set_NumberDecimalSeparator(",");
        provider.set_NumberGroupSeparator(".");
        provider.set_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.get_CurrentCulture().get_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);
    } //main
} //ToDoubleProviderDemo

/*
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 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft