Export (0) Print
Expand All

Double.ToString Method (IFormatProvider)

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

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

'Declaration
Public Function ToString ( _
	provider As IFormatProvider _
) As String
'Usage
Dim instance As Double
Dim provider As IFormatProvider
Dim returnValue As String

returnValue = instance.ToString(provider)
public final String ToString (
	IFormatProvider provider
)
public final function ToString (
	provider : IFormatProvider
) : String
Not applicable.

Parameters

provider

An IFormatProvider that supplies culture-specific formatting information.

Return Value

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

The return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or a string of the form:

[sign]integral-digits[.[fractional-digits]][e[sign]exponential-digits]

Optional items are framed in square brackets ([ and ]). Items containing the term "digits" consist of a series of numeric characters ranging from 0 to 9.

sign

A negative sign or positive sign symbol.

integral-digits

A series of digits specifying the integral part of the number. Integral-digits can be absent if there are fractional-digits.

'.'

A culture-specific decimal point symbol.

fractional-digits

A series of digits specifying the fractional part of the number.

'e'

A lowercase character 'e', indicating exponential (scientific) notation.

exponential-digits

A series of digits specifying an exponent.

Some examples of the return value are "100", "-123,456,789", "123.45e+6", "500", "3.1416", "600", "-0.123", and "-Infinity".

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 instance that obtains a NumberFormatInfo object that supplies culture-specific format information. If provider is a null reference (Nothing in Visual Basic), the return value is formatted with NumberFormatInfo data for the current culture.

The following code example formats an integral and a floating-point numeric value using the thread current culture, a specified culture, and all the standard numeric format specifiers. This code example uses two particular numeric types, but would yield similar results for any of the numeric base types (Byte, SByte, Int16, Int32, Int64, UInt16, UInt32, UInt64, Decimal, Single, and Double).

' This code example demonstrates the ToString(String) and 
' ToString(String, IFormatProvider) methods for integral and
' floating-point numbers, in conjunction with the standard 
' numeric format specifiers.
' This code example uses the System.Int32 integral type and 
' the System.Double floating-point type, but would yield 
' similar results for any of the numeric types. The integral 
' numeric types are System.Byte, SByte, Int16, Int32, Int64, 
' UInt16, UInt32, and UInt64. The floating-point numeric types 
' are Decimal, Single, and Double.

Imports System
Imports System.Globalization
Imports System.Threading

Class Sample
    Public Shared Sub Main() 
        ' Format a negative integer or floating-point number in various ways.
        Dim integralVal As Integer = -12345
        Dim floatingVal As Double  = -1234.567
        
        Dim msgCurrency As String =    "(C) Currency: . . . . . . "
        Dim msgDecimal As String =     "(D) Decimal:. . . . . . . "
        Dim msgScientific As String =  "(E) Scientific: . . . . . "
        Dim msgFixedPoint As String =  "(F) Fixed point:. . . . . "
        Dim msgGeneral As String =     "(G) General (default):. . "
        Dim msgNumber As String =      "(N) Number: . . . . . . . "
        Dim msgPercent As String =     "(P) Percent:. . . . . . . "
        Dim msgRoundTrip As String =   "(R) Round-trip: . . . . . "
        Dim msgHexadecimal As String = "(X) Hexadecimal:. . . . . "
        
        Dim msg1 As String = "Use ToString(String) and the current thread culture." + vbLf
        Dim msg2 As String = "Use ToString(String, IFormatProvider) and a specified culture." + vbLf
        Dim msgCulture As String = "Culture:"
        Dim msgIntegralVal As String = "Integral value:"
        Dim msgFloatingVal As String = "Floating-point value:"
        
        Dim ci As CultureInfo
        '
        Console.Clear()
        Console.WriteLine("Standard Numeric Format Specifiers:" & vbLf)
        ' Display the values.
        Console.WriteLine(msg1)
        
        ' Display the thread current culture, which is used to format the values.
        ci = Thread.CurrentThread.CurrentCulture
        Console.WriteLine("{0,-26}{1}", msgCulture, ci.DisplayName)
        
        ' Display the integral and floating-point values.
        Console.WriteLine("{0,-26}{1}", msgIntegralVal, integralVal)
        Console.WriteLine("{0,-26}{1}", msgFloatingVal, floatingVal)
        Console.WriteLine()
        
        ' Use the format specifiers that are only for integral types.
        Console.WriteLine("Format specifiers only for integral types:")
        Console.WriteLine(msgDecimal     & integralVal.ToString("D"))
        Console.WriteLine(msgHexadecimal & integralVal.ToString("X"))
        Console.WriteLine()
        
        ' Use the format specifier that is only for the Single and Double 
        ' floating-point types.
        Console.WriteLine("Format specifier only for the Single and Double types:")
        Console.WriteLine(msgRoundTrip   & floatingVal.ToString("R"))
        Console.WriteLine()
        
        ' Use the format specifiers that are for integral or floating-point types.
        Console.WriteLine("Format specifiers for integral or floating-point types:")
        Console.WriteLine(msgCurrency    & floatingVal.ToString("C"))
        Console.WriteLine(msgScientific  & floatingVal.ToString("E"))
        Console.WriteLine(msgFixedPoint  & floatingVal.ToString("F"))
        Console.WriteLine(msgGeneral     & floatingVal.ToString("G"))
        Console.WriteLine(msgNumber      & floatingVal.ToString("N"))
        Console.WriteLine(msgPercent     & floatingVal.ToString("P"))
        Console.WriteLine()
        
        ' Display the same values using a CultureInfo object. The CultureInfo class 
        ' implements IFormatProvider.
        Console.WriteLine(msg2)
        
        ' Display the culture used to format the values. 
        ' Create a European culture and change its currency symbol to "euro" because 
        ' this particular code example uses a thread current UI culture that cannot 
        ' display the euro symbol (€).
        ci = New CultureInfo("de-DE")
        ci.NumberFormat.CurrencySymbol = "euro"
        Console.WriteLine("{0,-26}{1}", msgCulture, ci.DisplayName)
        
        ' Display the integral and floating-point values.
        Console.WriteLine("{0,-26}{1}", msgIntegralVal, integralVal)
        Console.WriteLine("{0,-26}{1}", msgFloatingVal, floatingVal)
        Console.WriteLine()
        
        ' Use the format specifiers that are only for integral types.
        Console.WriteLine("Format specifiers only for integral types:")
        Console.WriteLine(msgDecimal     & integralVal.ToString("D", ci))
        Console.WriteLine(msgHexadecimal & integralVal.ToString("X", ci))
        Console.WriteLine()
        
        ' Use the format specifier that is only for the Single and Double 
        ' floating-point types.
        Console.WriteLine("Format specifier only for the Single and Double types:")
        Console.WriteLine(msgRoundTrip   & floatingVal.ToString("R", ci))
        Console.WriteLine()
        
        ' Use the format specifiers that are for integral or floating-point types.
        Console.WriteLine("Format specifiers for integral or floating-point types:")
        Console.WriteLine(msgCurrency    & floatingVal.ToString("C", ci))
        Console.WriteLine(msgScientific  & floatingVal.ToString("E", ci))
        Console.WriteLine(msgFixedPoint  & floatingVal.ToString("F", ci))
        Console.WriteLine(msgGeneral     & floatingVal.ToString("G", ci))
        Console.WriteLine(msgNumber      & floatingVal.ToString("N", ci))
        Console.WriteLine(msgPercent     & floatingVal.ToString("P", ci))
        Console.WriteLine()
    End Sub 'Main
End Class 'Sample
'
'This code example produces the following results:
'
'Standard Numeric Format Specifiers:
'
'Use ToString(String) and the current thread culture.
'
'Culture:                  English (United States)
'Integral value:           -12345
'Floating-point value:     -1234.567
'
'Format specifiers only for integral types:
'(D) Decimal:. . . . . . . -12345
'(X) Hexadecimal:. . . . . FFFFCFC7
'
'Format specifier only for the Single and Double types:
'(R) Round-trip: . . . . . -1234.567
'
'Format specifiers for integral or floating-point types:
'(C) Currency: . . . . . . ($1,234.57)
'(E) Scientific: . . . . . -1.234567E+003
'(F) Fixed point:. . . . . -1234.57
'(G) General (default):. . -1234.567
'(N) Number: . . . . . . . -1,234.57
'(P) Percent:. . . . . . . -123,456.70 %
'
'Use ToString(String, IFormatProvider) and a specified culture.
'
'Culture:                  German (Germany)
'Integral value:           -12345
'Floating-point value:     -1234.567
'
'Format specifiers only for integral types:
'(D) Decimal:. . . . . . . -12345
'(X) Hexadecimal:. . . . . FFFFCFC7
'
'Format specifier only for the Single and Double types:
'(R) Round-trip: . . . . . -1234,567
'
'Format specifiers for integral or floating-point types:
'(C) Currency: . . . . . . -1.234,57 euro
'(E) Scientific: . . . . . -1,234567E+003
'(F) Fixed point:. . . . . -1234,57
'(G) General (default):. . -1234,567
'(N) Number: . . . . . . . -1.234,57
'(P) Percent:. . . . . . . -123.456,70%
'

The following code 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
        Get
            Return m_value
        End Get
        Set(ByVal Value As Double)
            m_value = Value
        End Set
    End Property

    Public Property Celsius() As Double
        Get
            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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, 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