Aracılığıyla paylaş


Double.ToString Method (String, IFormatProvider)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Function ToString ( _
    format As String, _
    provider As IFormatProvider _
) As String
[SecuritySafeCriticalAttribute]
public string ToString(
    string format,
    IFormatProvider provider
)

Parameters

  • format
    Type: System.String
    A standard or custom numeric format string (see Remarks).

Return Value

Type: System.String
The string representation of the value of this instance as specified by format and provider.

Implements

IFormattable.ToString(String, IFormatProvider)

Remarks

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 nulla null reference (Nothing in Visual Basic) 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 nulla null reference (Nothing in Visual Basic), 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.

The provider parameter is an IFormatProvider instance that obtains a NumberFormatInfo object that supplies culture-specific format information. If provider is nulla null reference (Nothing in Visual Basic), the return value is formatted with NumberFormatInfo data for the current culture.

Examples

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 = New CultureInfo("eu-ES")
outputBlock.Text &= value.ToString(specifier, culture) & vbCrLf
' Displays:    16325,62901
outputBlock.Text &= value.ToString(specifier, CultureInfo.InvariantCulture) & vbCrLf
' Displays:    16325.62901

specifier = "C"
culture = New CultureInfo("en-US")
outputBlock.Text &= value.ToString(specifier, culture) & vbCrLf
' Displays:    $16,325.63
culture = New CultureInfo("en-GB")
outputBlock.Text &= value.ToString(specifier, culture) & vbCrLf
' Displays:    ?16,325.63

specifier = "E04"
culture = New CultureInfo("sv-SE")
outputBlock.Text &= value.ToString(specifier, culture) & vbCrLf
' Displays: 1,6326E+004   
culture = New CultureInfo("en-NZ")
outputBlock.Text &= value.ToString(specifier, culture) & vbCrLf
' Displays:    1.6326E+004   

specifier = "F"
culture = New CultureInfo("fr-FR")
outputBlock.Text &= value.ToString(specifier, culture) & vbCrLf
' Displays:    16325,63
culture = New CultureInfo("en-CA")
outputBlock.Text &= value.ToString(specifier, culture) & vbCrLf
' Displays:    16325.63

specifier = "N"
culture = New CultureInfo("es-ES")
outputBlock.Text &= value.ToString(specifier, culture) & vbCrLf
' Displays:    16.325,63
culture = New CultureInfo("fr-CA")
outputBlock.Text &= value.ToString(specifier, culture) & vbCrLf
' Displays:    16 325,63

specifier = "P"
culture = CultureInfo.InvariantCulture
outputBlock.Text &= (value / 10000).ToString(specifier, culture) & vbCrLf
' Displays:    163.26 %
culture = New CultureInfo("ar-EG")
outputBlock.Text &= (value / 10000).ToString(specifier, culture) & vbCrLf
' Displays:    163.256 %
double value = 16325.62901;
string specifier;
CultureInfo culture;

// Use standard numeric format specifiers.
specifier = "G";
culture = new CultureInfo("eu-ES");
outputBlock.Text += value.ToString(specifier, culture) + "\n";
// Displays:    16325,62901
outputBlock.Text += value.ToString(specifier, CultureInfo.InvariantCulture) + "\n";
// Displays:    16325.62901

specifier = "C";
culture = new CultureInfo("en-US");
outputBlock.Text += value.ToString(specifier, culture) + "\n";
// Displays:    $16,325.63
culture = new CultureInfo("en-GB");
outputBlock.Text += value.ToString(specifier, culture) + "\n";
// Displays:    £16,325.63

specifier = "E04";
culture = new CultureInfo("sv-SE");
outputBlock.Text += value.ToString(specifier, culture) + "\n";
// Displays: 1,6326E+004   
culture = new CultureInfo("en-NZ");
outputBlock.Text += value.ToString(specifier, culture) + "\n";
// Displays:    1.6326E+004   

specifier = "F";
culture = new CultureInfo("fr-FR");
outputBlock.Text += value.ToString(specifier, culture) + "\n";
// Displays:    16325,63
culture = new CultureInfo("en-CA");
outputBlock.Text += value.ToString(specifier, culture) + "\n";
// Displays:    16325.63

specifier = "N";
culture = new CultureInfo("es-ES");
outputBlock.Text += value.ToString(specifier, culture) + "\n";
// Displays:    16.325,63
culture = new CultureInfo("fr-CA");
outputBlock.Text += value.ToString(specifier, culture) + "\n";
// Displays:    16 325,63

specifier = "P";
culture = CultureInfo.InvariantCulture;
outputBlock.Text += (value / 10000).ToString(specifier, culture) + "\n";
// Displays:    163.26 %
culture = new CultureInfo("ar-EG");
outputBlock.Text += (value / 10000).ToString(specifier, culture) + "\n";
// 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
      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
public class Temperature : IFormattable
{
   // IFormattable.ToString implementation.
   public string ToString(string format, IFormatProvider provider)
   {
      if (format != null)
      {
         if (format.Equals("F"))
         {
            return String.Format("{0}'F", this.Value.ToString());
         }
         if (format.Equals("C"))
         {
            return String.Format("{0}'C", this.Celsius.ToString());
         }
      }

      return m_value.ToString(format, provider);
   }

   // The value holder
   protected double m_value;

   public double Value
   {
      get
      {
         return m_value;
      }
      set
      {
         m_value = value;
      }
   }

   public double Celsius
   {
      get
      {
         return (m_value - 32.0) / 1.8;
      }
      set
      {
         m_value = 1.8 * value + 32.0;
      }
   }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.