Double.ToString Method (String, IFormatProvider)


Converts the numeric value of this instance to its equivalent string representation using the specified format and culture-specific format information.

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

Public Function ToString (
	format As String,
	provider As IFormatProvider
) As String


Type: System.String

A numeric format string.

Type: System.IFormatProvider

An object that supplies culture-specific formatting information.

Return Value

Type: System.String

The string representation of the value of this instance as specified by format and provider.

The ToString(String, IFormatProvider) method formats a Double value in a specified format of a specified culture. If you want to specify a different format or culture, use the other overloads of the ToString method, as follows:

To use format

For culture

Use the overload

Default ("G") format

Default (current) culture


Default ("G") format

A specific culture


A specific format or precision

Default (current) culture


The return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or the string representation of a number, as specified by format.

The format parameter can be any valid standard numeric format specifier except for D and X, as well as any combination of custom numeric format specifiers. If format is null or an empty string, the return value for this instance is formatted with the general numeric format specifier ("G").

The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:

The provider parameter is an IFormatProvider implementation whose GetFormat method returns a NumberFormatInfo object. Typically, provider is a CultureInfo object or a NumberFormatInfo object. The provider parameter supplies culture-specific information used in formatting. If provider is null, the return value is formatted using the NumberFormatInfo object for the current culture.

By default, the return value only contains 15 digits of precision although a maximum of 17 digits is maintained internally. If the value of this instance has greater than 15 digits, ToString returns PositiveInfinitySymbol or NegativeInfinitySymbol instead of the expected number. If you require more precision, specify format with the "G17" format specification, which always returns 17 digits of precision, or "R", which returns 15 digits if the number can be represented with that precision or 17 digits if the number can only be represented with maximum precision.

Notes to Callers:

In some cases, Double values formatted with the "R" standard numeric format string do not successfully round-trip if compiled using the /platform:x64 or /platform:anycpu switches and run on 64-bit systems. To work around this problem, you can format Double values by using the "G17" standard numeric format string. The following example uses the "R" format string with a Double value that does not round-trip successfully, and also uses the "G17" format string to successfully round-trip the original value.

Imports System.Globalization

Module Example
   Public Sub Main()
      Console.WriteLine("Attempting to round-trip a Double with 'R':")
      Dim initialValue As Double = 0.6822871999174
      Dim valueString As String = initialValue.ToString("R",
      Dim roundTripped As Double = Double.Parse(valueString,
      Console.WriteLine("{0:R} = {1:R}: {2}",
                        initialValue, roundTripped, initialValue.Equals(roundTripped))

      Console.WriteLine("Attempting to round-trip a Double with 'G17':")
      Dim valueString17 As String = initialValue.ToString("G17",
      Dim roundTripped17 As Double = double.Parse(valueString17,
      Console.WriteLine("{0:R} = {1:R}: {2}",
                        initialValue, roundTripped17, initialValue.Equals(roundTripped17))
   End Sub
End Module
' If compiled to an application that targets anycpu or x64 and run on an x64 system,
' the example displays the following output:
'       Attempting to round-trip a Double with 'R':
'       0.6822871999174 = 0.68228719991740006: False
'       Attempting to round-trip a Double with 'G17':
'       0.6822871999174 = 0.6822871999174: True

The following example displays a Double value using each of the supported standard numeric format specifiers for several different cultures.

Dim value As Double = 16325.62901
Dim specifier As String
Dim culture As CultureInfo

' Use standard numeric format specifiers.
specifier = "G"
culture = CultureInfo.CreateSpecificCulture("eu-ES")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    16325,62901
Console.WriteLine(value.ToString(specifier, CultureInfo.InvariantCulture))
' Displays:    16325.62901

specifier = "C"
culture = CultureInfo.CreateSpecificCulture("en-US")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    $16,325.63
culture = CultureInfo.CreateSpecificCulture("en-GB")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    £16,325.63

specifier = "E04"
culture = CultureInfo.CreateSpecificCulture("sv-SE")
Console.WriteLine(value.ToString(specifier, culture))
' Displays: 1,6326E+004   
 culture = CultureInfo.CreateSpecificCulture("en-NZ")
 Console.WriteLine(value.ToString(specifier, culture))
' Displays:    1.6326E+004   

specifier = "F"
culture = CultureInfo.CreateSpecificCulture("fr-FR")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    16325,63
culture = CultureInfo.CreateSpecificCulture("en-CA")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    16325.63

specifier = "N"
culture = CultureInfo.CreateSpecificCulture("es-ES")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    16.325,63
culture = CultureInfo.CreateSpecificCulture("fr-CA")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    16 325,63

specifier = "P"
culture = CultureInfo.InvariantCulture
Console.WriteLine((value/10000).ToString(specifier, culture))
' Displays:    163.26 %
culture = CultureInfo.CreateSpecificCulture("ar-EG")
Console.WriteLine((value/10000).ToString(specifier, culture))
' Displays:    163.256 %

The following example illustrates the use of ToString, taking a String and an IFormatProvider as parameters.

Public Class Temperature
    Implements IFormattable

    Public Overloads Function ToString(ByVal format As String, ByVal provider As IFormatProvider) As String _
        Implements IFormattable.ToString

        If Not (format Is Nothing) Then
            If format.Equals("F") Then
                Return [String].Format("{0}'F", Me.Value.ToString())
            End If
            If format.Equals("C") Then
                Return [String].Format("{0}'C", Me.Celsius.ToString())
            End If
        End If

        Return m_value.ToString(format, provider)
    End Function

    ' The value holder
    Protected m_value As Double

    Public Property Value() As Double
            Return m_value
        End Get
        Set(ByVal Value As Double)
            m_value = Value
        End Set
    End Property

    Public Property Celsius() As Double
            Return (m_value - 32) / 1.8
        End Get
        Set(ByVal Value As Double)
            m_value = Value * 1.8 + 32
        End Set
    End Property
End Class

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top