Double.ToString Method (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 culture-specific format information.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
<SecuritySafeCriticalAttribute> _
Public Function ToString ( _
provider As IFormatProvider _
) As String
[SecuritySafeCriticalAttribute]
public string ToString(
IFormatProvider provider
)
Parameters
- provider
Type: System.IFormatProvider
An IFormatProvider that supplies culture-specific formatting information.
Return Value
Type: System.String
The string representation of the value of this instance as specified by provider.
Implements
Remarks
The return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or a string of the form:
[sign]integral-digits[.[fractional-digits]][e[sign]exponential-digits]
Optional elements are framed in square brackets ([ and ]). All digits elements consist of a series of numeric characters ranging from 0 to 9. The elements listed in the following table are supported.
Element |
Description |
---|---|
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:
For more information about numeric format specifiers, see Standard Numeric Format Strings and Custom Numeric Format Strings.
For more information about formatting, see Formatting Types.
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.
To convert a Double value to its string representation using a specified culture and a specific format string, call the Double.ToString(String, IFormatProvider) method.
Examples
The following example displays the string representation of two Double values using CultureInfo objects that represent several different cultures.
Dim value As Double
value = -16325.62015
' Display value using the invariant culture.
outputBlock.Text &= value.ToString(CultureInfo.InvariantCulture) & vbCrLf
' Display value using the en-GB culture.
outputBlock.Text &= value.ToString(New CultureInfo("en-GB")) & vbCrLf
' Display value using the de-DE culture.
outputBlock.Text &= value.ToString(New CultureInfo("de-DE")) & vbCrLf
value = 1.6034125E+25
' Display value using the invariant culture.
outputBlock.Text &= value.ToString(CultureInfo.InvariantCulture) & vbCrLf
' Display value using the en-GB culture.
outputBlock.Text &= value.ToString(New CultureInfo("en-GB")) & vbCrLf
' Display value using the de-DE culture.
outputBlock.Text &= value.ToString(New CultureInfo("de-DE")) & vbCrLf
' This example displays the following output:
' -16325.62015
' -16325.62015
' -16325,62015
' 1.6034125E+25
' 1.6034125E+25
' 1,6034125E+25
double value;
value = -16325.62015;
// Display value using the invariant culture.
outputBlock.Text += value.ToString(CultureInfo.InvariantCulture) + "\n";
// Display value using the en-GB culture.
outputBlock.Text += value.ToString(new CultureInfo("en-GB")) + "\n";
// Display value using the de-DE culture.
outputBlock.Text += value.ToString(new CultureInfo("de-DE")) + "\n";
value = 16034.125E21;
// Display value using the invariant culture.
outputBlock.Text += value.ToString(CultureInfo.InvariantCulture) + "\n";
// Display value using the en-GB culture.
outputBlock.Text += value.ToString(new CultureInfo("en-GB")) + "\n";
// Display value using the de-DE culture.
outputBlock.Text += value.ToString(new CultureInfo("de-DE")) + "\n";
// This example displays the following output:
// -16325.62015
// -16325.62015
// -16325,62015
// 1.6034125E+25
// 1.6034125E+25
// 1,6034125E+25
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.
See Also