Freigeben über


Double.ToString Method (String)

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.

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

Syntax

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

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.

Exceptions

Exception Condition
FormatException

format is invalid.

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 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 return value is formatted using the NumberFormatInfo object for the current culture. To apply the formatting conventions of a specified culture, call the Double.ToString(String, IFormatProvider) method.

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 return value is formatted with NumberFormatInfo data for the current culture.

Examples

The following example displays several Double values using each of the supported standard numeric format specifiers together with two custom numeric format strings. One of those custom format strings illustrates how to pad a Single value with leading zeroes. In converting the numeric values to strings, the example uses the formatting conventions of the en-US culture.

Dim numbers() As Double = {1054.32179, -195489100.8377, 1.0437E+21, _
                         -0.000010573}
Dim specifiers() As String = {"C", "E", "e", "F", "G", "N", "P", _
                               "R", "#,000.000", "0.###E-000", _
                               "000,000,000,000.00###" }
For Each number As Double In numbers
   outputBlock.Text += String.Format("Formatting of {0}:", number) & vbCrLf
   For Each specifier As String In specifiers
      outputBlock.Text += String.Format("   {0,5}: {1}", _
                        specifier, number.ToString(specifier)) & vbCrLf
   Next
   outputBlock.Text &= vbCrLf
Next
' The example displays the following output:
'       Formatting of 1054.32179:
'              C: $1,054.32
'              E: 1.054322E+003
'              e: 1.054322e+003
'              F: 1054.32
'              G: 1054.32179
'              N: 1,054.32
'              P: 105,432.18 %
'              R: 1054.32179
'          #,000.000: 1,054.322
'          0.###E-000: 1.054E003
'          000,000,000,000.00###: 000,000,001,054.322
'       
'       Formatting of -195489100.8377:
'              C: ($195,489,100.84)
'              E: -1.954891E+008
'              e: -1.954891e+008
'              F: -195489100.84
'              G: -195489100.8377
'              N: -195,489,100.84
'              P: -19,548,910,083.77 %
'              R: -195489100.8377
'          #,000.000: -195,489,100.838
'          0.###E-000: -1.955E008
'          000,000,000,000.00###: -000,195,489,100.00
'       
'       Formatting of 1.0437E+21:
'              C: $1,043,700,000,000,000,000,000.00
'              E: 1.043700E+021
'              e: 1.043700e+021
'              F: 1043700000000000000000.00
'              G: 1.0437E+21
'              N: 1,043,700,000,000,000,000,000.00
'              P: 104,370,000,000,000,000,000,000.00 %
'              R: 1.0437E+21
'          #,000.000: 1,043,700,000,000,000,000,000.000
'          0.###E-000: 1.044E021
'          000,000,000,000.00###: 1,043,700,000,000,000,000,000.00
'       
'       Formatting of -1.0573E-05:
'              C: $0.00
'              E: -1.057300E-005
'              e: -1.057300e-005
'              F: 0.00
'              G: -1.0573E-05
'              N: 0.00
'              P: 0.00 %
'              R: -1.0573E-05
'          #,000.000: 000.000
'          0.###E-000: -1.057E-005
'          000,000,000,000.00###: -000,000,000,000.00001 
double[] numbers = {1054.32179, -195489100.8377, 1.0437E21, 
                   -1.0573e-05};
string[] specifiers = { "C", "E", "e", "F", "G", "N", "P", 
                        "R", "#,000.000", "0.###E-000",
                        "000,000,000,000.00###" };
foreach (double number in numbers)
{
   outputBlock.Text += String.Format("Formatting of {0}:", number) + "\n";
   foreach (string specifier in specifiers)
      outputBlock.Text += String.Format("   {0,5}: {1}",
                        specifier, number.ToString(specifier)) + "\n";

   outputBlock.Text += "\n";
}
// The example displays the following output:
//       Formatting of 1054.32179:
//              C: $1,054.32
//              E: 1.054322E+003
//              e: 1.054322e+003
//              F: 1054.32
//              G: 1054.32179
//              N: 1,054.32
//              P: 105,432.18 %
//              R: 1054.32179
//          #,000.000: 1,054.322
//          0.###E-000: 1.054E003
//          000,000,000,000.00###: 000,000,001,054.322
//       
//       Formatting of -195489100.8377:
//              C: ($195,489,100.84)
//              E: -1.954891E+008
//              e: -1.954891e+008
//              F: -195489100.84
//              G: -195489100.8377
//              N: -195,489,100.84
//              P: -19,548,910,083.77 %
//              R: -195489100.8377
//          #,000.000: -195,489,100.838
//          0.###E-000: -1.955E008
//          000,000,000,000.00###: -000,195,489,100.00
//       
//       Formatting of 1.0437E+21:
//              C: $1,043,700,000,000,000,000,000.00
//              E: 1.043700E+021
//              e: 1.043700e+021
//              F: 1043700000000000000000.00
//              G: 1.0437E+21
//              N: 1,043,700,000,000,000,000,000.00
//              P: 104,370,000,000,000,000,000,000.00 %
//              R: 1.0437E+21
//          #,000.000: 1,043,700,000,000,000,000,000.000
//          0.###E-000: 1.044E021
//          000,000,000,000.00###: 1,043,700,000,000,000,000,000.00
//       
//       Formatting of -1.0573E-05:
//              C: $0.00
//              E: -1.057300E-005
//              e: -1.057300e-005
//              F: 0.00
//              G: -1.0573E-05
//              N: 0.00
//              P: 0.00 %
//              R: -1.0573E-05
//          #,000.000: 000.000
//          0.###E-000: -1.057E-005
//          000,000,000,000.00###: -000,000,000,000.00001 

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.