Export (0) Print
Expand All

Convert.ToDouble Method

Converts a specified value to a double-precision floating point number.

Overload List

Converts the value of the specified Boolean value to the equivalent double-precision floating point number.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDouble(Boolean) As Double
[C#] public static double ToDouble(bool);
[C++] public: static double ToDouble(bool);
[JScript] public static function ToDouble(Boolean) : double;

Converts the value of the specified 8-bit unsigned integer to the equivalent double-precision floating point number.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDouble(Byte) As Double
[C#] public static double ToDouble(byte);
[C++] public: static double ToDouble(unsigned char);
[JScript] public static function ToDouble(Byte) : double;

Calling this method always throws InvalidCastException.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDouble(Char) As Double
[C#] public static double ToDouble(char);
[C++] public: static double ToDouble(__wchar_t);
[JScript] public static function ToDouble(Char) : double;

Calling this method always throws InvalidCastException.

[Visual Basic] Overloads Public Shared Function ToDouble(DateTime) As Double
[C#] public static double ToDouble(DateTime);
[C++] public: static double ToDouble(DateTime);
[JScript] public static function ToDouble(DateTime) : double;

Converts the value of the specified Decimal number to an equivalent double-precision floating point number.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDouble(Decimal) As Double
[C#] public static double ToDouble(decimal);
[C++] public: static double ToDouble(Decimal);
[JScript] public static function ToDouble(Decimal) : double;

Returns the specified double-precision floating point number; no actual conversion is performed.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDouble(Double) As Double
[C#] public static double ToDouble(double);
[C++] public: static double ToDouble(double);
[JScript] public static function ToDouble(double) : double;

Converts the value of the specified 16-bit signed integer to an equivalent double-precision floating point number.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDouble(Short) As Double
[C#] public static double ToDouble(short);
[C++] public: static double ToDouble(short);
[JScript] public static function ToDouble(Int16) : double;

Converts the value of the specified 32-bit signed integer to an equivalent double-precision floating point number.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDouble(Integer) As Double
[C#] public static double ToDouble(int);
[C++] public: static double ToDouble(int);
[JScript] public static function ToDouble(int) : double;

Converts the value of the specified 64-bit signed integer to an equivalent double-precision floating point number.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDouble(Long) As Double
[C#] public static double ToDouble(long);
[C++] public: static double ToDouble(__int64);
[JScript] public static function ToDouble(long) : double;

Converts the value of the specified Object to a double-precision floating point number.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDouble(Object) As Double
[C#] public static double ToDouble(object);
[C++] public: static double ToDouble(Object*);
[JScript] public static function ToDouble(Object) : double;

Converts the value of the specified 8-bit signed integer to the equivalent double-precision floating point number. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDouble(SByte) As Double
[C#] public static double ToDouble(sbyte);
[C++] public: static double ToDouble(char);
[JScript] public static function ToDouble(SByte) : double;

Converts the value of the specified single-precision floating point number to an equivalent double-precision floating point number.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDouble(Single) As Double
[C#] public static double ToDouble(float);
[C++] public: static double ToDouble(float);
[JScript] public static function ToDouble(float) : double;

Converts the specified String representation of a number to an equivalent double-precision floating point number.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDouble(String) As Double
[C#] public static double ToDouble(string);
[C++] public: static double ToDouble(String*);
[JScript] public static function ToDouble(String) : double;

Converts the value of the specified 16-bit unsigned integer to the equivalent double-precision floating point number. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDouble(UInt16) As Double
[C#] public static double ToDouble(ushort);
[C++] public: static double ToDouble(unsigned short);
[JScript] public static function ToDouble(UInt16) : double;

Converts the value of the specified 32-bit unsigned integer to an equivalent double-precision floating point number. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDouble(UInt32) As Double
[C#] public static double ToDouble(uint);
[C++] public: static double ToDouble(unsigned int);
[JScript] public static function ToDouble(UInt32) : double;

Converts the value of the specified 64-bit unsigned integer to an equivalent double-precision floating point number. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDouble(UInt64) As Double
[C#] public static double ToDouble(ulong);
[C++] public: static double ToDouble(unsigned __int64);
[JScript] public static function ToDouble(UInt64) : double;

Converts the value of the specified Object to an double-precision floating point number using the specified culture-specific formatting information.

[Visual Basic] Overloads Public Shared Function ToDouble(Object, IFormatProvider) As Double
[C#] public static double ToDouble(object, IFormatProvider);
[C++] public: static double ToDouble(Object*, IFormatProvider*);
[JScript] public static function ToDouble(Object, IFormatProvider) : double;

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDouble(String, IFormatProvider) As Double
[C#] public static double ToDouble(string, IFormatProvider);
[C++] public: static double ToDouble(String*, IFormatProvider*);
[JScript] public static function ToDouble(String, IFormatProvider) : double;

Example

[Visual Basic, C#, C++] The following code example converts String representations of Double values with the ToDouble method, using an IFormatProvider object.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of ToDouble. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
' Example of the Convert.ToDouble( String ) and 
' Convert.ToDouble( String, IFormatProvider ) methods.
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic

Module ToDoubleProviderDemo

    Dim formatter As String = "{0,-22}{1,-20}{2}"

    ' Get the exception type name; remove the namespace prefix.
    Function GetExceptionType( ex As Exception ) As String

        Dim exceptionType   As String = ex.GetType( ).ToString( )
        GetExceptionType = exceptionType.Substring( _
            exceptionType.LastIndexOf( "."c ) + 1 )
    End Function

    Sub ConvertToDouble( numericStr As String, _
        provider As IFormatProvider )

        Dim defaultValue    As Object
        Dim providerValue   As Object

        ' Convert numericStr to Double without a format provider.
        Try
            defaultValue = Convert.ToDouble( numericStr )
        Catch ex As Exception
            defaultValue = GetExceptionType( ex )
        End Try

        ' Convert numericStr to Double with a format provider.
        Try
            providerValue = Convert.ToDouble( numericStr, provider )
        Catch ex As Exception
            providerValue = GetExceptionType( ex )
        End Try

        Console.WriteLine( formatter, numericStr, _
            defaultValue, providerValue )
    End Sub

    Sub Main( )

        ' Create a NumberFormatInfo object and set several of its
        ' properties that apply to numbers.
        Dim provider  As NumberFormatInfo = new NumberFormatInfo( )

        provider.NumberDecimalSeparator = ","
        provider.NumberGroupSeparator = "."
        provider.NumberGroupSizes = New Integer( ) { 3 }

        Console.WriteLine( "This example of" & vbCrLf & _
            "  Convert.ToDouble( String ) and " & vbCrLf & _
            "  Convert.ToDouble( String, IFormatProvider ) " & _
            vbCrLf & "generates the " & _
            "following output when run in the [{0}] culture.", _
            CultureInfo.CurrentCulture.Name )
        Console.WriteLine( vbCrLf & _
            "Several strings are converted to Double values, " & _
            "using " & vbCrLf & "default formatting " & _
            "and a NumberFormatInfo object." & vbCrLf )
        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 )
    End Sub 
End Module 

' 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

[C#] 
// 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
*/

[C++] 
// Example of the Convert::ToDouble( String ) and 
// Convert::ToDouble( String, IFormatProvider ) methods.
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;

const __wchar_t* formatter = L"{0,-22}{1,-20}{2}";

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

void ConvertToDouble( String* numericStr, 
    IFormatProvider* provider )
{
    Object* defaultValue;
    Object* providerValue;

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

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

    Console::WriteLine( new String( formatter ), numericStr, 
        defaultValue, providerValue );
}

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

    provider->NumberDecimalSeparator = S",";
    provider->NumberGroupSeparator = S".";
    Int32 sizes __gc [] = { 3 };
    provider->NumberGroupSizes = sizes;

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

    // Convert strings, with and without an IFormatProvider.
    ConvertToDouble( S"123456789", provider );
    ConvertToDouble( S"12345.6789", provider );
    ConvertToDouble( S"12345,6789", provider );
    ConvertToDouble( S"123,456.789", provider );
    ConvertToDouble( S"123.456,789", provider );
    ConvertToDouble( S"123,456,789.0123", provider );
    ConvertToDouble( S"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
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Convert Class | Convert Members | System Namespace

Show:
© 2014 Microsoft