Double.ToString Double.ToString Double.ToString Double.ToString Method

定義

將這個執行個體的數值轉換為它的相等字串表示。Converts the numeric value of this instance to its equivalent string representation.

多載

ToString(String, IFormatProvider) ToString(String, IFormatProvider) ToString(String, IFormatProvider) ToString(String, IFormatProvider)

使用指定的格式和特定文化特性格式資訊,將這個執行個體的數值轉換成它的相等字串表示。Converts the numeric value of this instance to its equivalent string representation using the specified format and culture-specific format information.

ToString(String) ToString(String) ToString(String) ToString(String)

使用指定格式,將這個執行個體的數值轉換成它的相等字串表示。Converts the numeric value of this instance to its equivalent string representation, using the specified format.

ToString(IFormatProvider) ToString(IFormatProvider) ToString(IFormatProvider) ToString(IFormatProvider)

使用指定的特定文化特性格式資訊,將這個執行個體的數值轉換成它的相等字串表示。Converts the numeric value of this instance to its equivalent string representation using the specified culture-specific format information.

ToString() ToString() ToString() ToString()

將這個執行個體的數值轉換為它的相等字串表示。Converts the numeric value of this instance to its equivalent string representation.

ToString(String, IFormatProvider) ToString(String, IFormatProvider) ToString(String, IFormatProvider) ToString(String, IFormatProvider)

使用指定的格式和特定文化特性格式資訊,將這個執行個體的數值轉換成它的相等字串表示。Converts the numeric value of this instance to its equivalent string representation using the specified format and culture-specific format information.

public:
 virtual System::String ^ ToString(System::String ^ format, IFormatProvider ^ provider);
public string ToString (string format, IFormatProvider provider);
override this.ToString : string * IFormatProvider -> string
Public Function ToString (format As String, provider As IFormatProvider) As String

參數

format
String String String String

數值格式字串。A numeric format string.

provider
IFormatProvider IFormatProvider IFormatProvider IFormatProvider

物件,提供特定文化特性格式資訊。An object that supplies culture-specific formatting information.

傳回

這個執行個體值的字串表示,如同 formatprovider 所指定。The string representation of the value of this instance as specified by format and provider.

實作

範例

下列範例會顯示Double值使用每個支援標準數值格式規範的數個不同的文化特性。The following example displays a Double value using each of the supported standard numeric format specifiers for several different cultures.

using namespace System;
using namespace System::Globalization;

int main(array<System::String ^> ^args)
{
   double value = 16325.62901;
   String^ specifier;
   CultureInfo^ culture;

   // 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 %

   return 0;
}
double value = 16325.62901;
string specifier;
CultureInfo culture;

// 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 %
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 %

下列範例示範如何將ToString、 正在接受StringIFormatProvider做為參數。The following example illustrates the use of ToString, taking a String and an IFormatProvider as parameters.

public ref class Temperature: public IFormattable
{
   // IFormattable.ToString implementation.
public:
   virtual String^ ToString( String^ format, IFormatProvider^ provider )
   {
      if ( format != nullptr )
      {
         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 );
   }

protected:
   // The value holder
   double m_value;

public:
   property double Value 
   {
      double get()
      {
         return m_value;
      }
      void set( double value )
      {
         m_value = value;
      }
   }

   property double Celsius 
   {
      double get()
      {
         return (m_value - 32.0) / 1.8;
      }
      void set( double value )
      {
         m_value = 1.8 * value + 32.0;
      }
   }
};
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;
		}
	}
}
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

備註

ToString(String, IFormatProvider)方法格式Double中指定的格式,指定文化特性值。The ToString(String, IFormatProvider) method formats a Double value in a specified format of a specified culture. 如果您想要指定不同的格式或文化特性,使用的其他多載ToString方法,如下所示: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
預設值 ("G") 格式Default ("G") format 預設值 (目前) 的文化特性Default (current) culture ToString()
預設值 ("G") 格式Default ("G") format 特定文化特性A specific culture ToString(IFormatProvider)
特定的格式或有效位數A specific format or precision 預設值 (目前) 的文化特性Default (current) culture ToString(String)

傳回值可以是PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbol,或數字,所指定的字串表示formatThe return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or the string representation of a number, as specified by format.

format參數可以是任何有效的標準數值格式規範,除了 D 和 X,以及自訂數值格式規範的任何組合。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. 如果formatnull或空字串,這個執行個體的傳回值會格式化為有一般的數值格式規範 ("G")。If format is null or an empty string, the return value for this instance is formatted with the general numeric format specifier ("G").

.NET Framework 會提供廣泛的格式化支援,這會在下列格式化主題中的更詳細地說明:The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:

provider參數是IFormatProvider實作其GetFormat方法會傳回NumberFormatInfo物件。The provider parameter is an IFormatProvider implementation whose GetFormat method returns a NumberFormatInfo object. 通常providerCultureInfo物件或NumberFormatInfo物件。Typically, provider is a CultureInfo object or a NumberFormatInfo object. provider參數提供格式化使用的特定文化特性資訊。The provider parameter supplies culture-specific information used in formatting. 如果providernull,傳回值的格式使用NumberFormatInfo目前的文化特性的物件。If provider is null, the return value is formatted using the NumberFormatInfo object for the current culture.

根據預設,傳回的值只包含 15 個位數的精確度雖然內部維護最多 17 個位數。By default, the return value only contains 15 digits of precision although a maximum of 17 digits is maintained internally. 如果這個執行個體的值必須大於 15 位數,ToString會傳回PositiveInfinitySymbolNegativeInfinitySymbol而不是預期的數目。If the value of this instance has greater than 15 digits, ToString returns PositiveInfinitySymbol or NegativeInfinitySymbol instead of the expected number. 如果您需要更多有效位數時,指定format"G17"格式規格,它一定會傳回 17 個位數的有效位數或"R",它會傳回 15 位數如果數目可以使用 17 位數來表示,如果只能是數字表示使用最大有效位數。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.

給呼叫者的注意事項

在某些情況下,使用 "R" 標準數值格式字串格式化的 Double 值,如果使用 /platform:x64/platform:anycpu 參數編譯並在 64 位元系統上執行,則不會成功地反覆存取。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. 若要解決這個問題,您可以使用 "G17" 標準數值格式字串來格式化 Double 值。To work around this problem, you can format Double values by using the "G17" standard numeric format string. 下列範例使用 "R" 格式字串,搭配不會成功反覆存取的 Double 值,並且也會使用 "G17" 格式字串來成功反覆存取原始值。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.

[!code-csharpSystem.Double.ToString#5] [!code-vbSystem.Double.ToString#5][!code-csharpSystem.Double.ToString#5] [!code-vbSystem.Double.ToString#5]

另請參閱

ToString(String) ToString(String) ToString(String) ToString(String)

使用指定格式,將這個執行個體的數值轉換成它的相等字串表示。Converts the numeric value of this instance to its equivalent string representation, using the specified format.

public:
 System::String ^ ToString(System::String ^ format);
public string ToString (string format);
override this.ToString : string -> string
Public Function ToString (format As String) As String

參數

format
String String String String

數值格式字串。A numeric format string.

傳回

這個執行個體值的字串表示,如同 format 所指定。The string representation of the value of this instance as specified by format.

例外狀況

format 無效。format is invalid.

範例

下列範例會定義數字的值和其格式化為貨幣值,藉由使用"C"標準數值格式字串和數值到三個小數位數為使用"N"標準數值格式字串。The following example defines a numeric value and formats it as a currency value by using the "C" standard numeric format string and as a numeric value to three decimal places by using the "N" standard numeric format string. 使用 EN-US 文化特性的慣例格式化的結果字串。The result strings are formatted by using the conventions of the en-US culture. 如需有關數值格式字串的詳細資訊,請參閱標準數值格式字串自訂數值格式字串For more information on numeric format strings, see Standard Numeric Format Strings and Custom Numeric Format Strings.

using System;

public class Example
{
   public static void Main()
   {
      float number = 1764.3789m;
      
      // Format as a currency value.
      Console.WriteLine(number.ToString("C"));
      
      // Format as a numeric value with 3 decimal places.
      Console.WriteLine(number.ToString("N3"));
   }
}
// The example displays the following output:
//       $1,764.38
//       1,764.379
Module Example
   Public Sub Main()
      Dim number As Double = 1764.3789
      
      ' Format as a currency value.
      Console.WriteLine(number.ToString("C"))
      
      ' Format as a numeric value with 3 decimal places.
      Console.WriteLine(number.ToString("N3"))
   End Sub
End Module
' The example displays the following output:
'       $1,764.38
'       1,764.379

下列範例會顯示數個Double使用支援標準數值格式規範,以及三個自訂數值格式字串值。The following example displays several Double values using the supported standard numeric format specifiers together with three custom numeric format strings. 這些自訂格式字串的其中一個說明如何將填補Single加上前置零的值。One of those custom format strings illustrates how to pad a Single value with leading zeros. 此外,此範例會使用有效位數規範,與每個標準格式規範,除了"R"。In addition, the example uses precision specifiers with each standard format specifier except for "R". 有效位數規範範圍從 0 到 3 的值。The values of the precision specifiers range from 0 to 3. 若要將數字的值轉換成字串,此範例會使用 EN-US 文化特性的格式化慣例。To convert the numeric values to strings, the example uses the formatting conventions of the en-US culture.

using namespace System;

void main()
{
   array<Double>^ numbers= {1054.32179, -195489100.8377, 1.0437E21, 
                            -1.0573e-05};
   array<String^>^ specifiers = { "C", "E", "e", "F", "G", "N", "P", 
                                  "R", "#,000.000", "0.###E-000",
                                  "000,000,000,000.00###" };
   for each (Double number in numbers)
   {  
      Console::WriteLine("Formatting of {0}:", number);
      for each (String^ specifier in specifiers) {
         Console::WriteLine("   {0,-22} {1}", 
                            specifier + ":", number.ToString(specifier));
         // Add precision specifiers from 0 to 3.
         if (specifier->Length == 1 & ! specifier->Equals("R")) {
            for (int precision = 0; precision <= 3; precision++) {
               String^ pSpecifier = String::Format("{0}{1}", specifier, precision);
               Console::WriteLine("   {0,-22} {1}", 
                                  pSpecifier + ":", number.ToString(pSpecifier));
            }    
            Console::WriteLine();
         }   
      }
      Console::WriteLine();
   }
}
// The example displays the following output:
//       Formatting of 1054.32179:
//          C:                     $1,054.32
//          C0:                    $1,054
//          C1:                    $1,054.3
//          C2:                    $1,054.32
//          C3:                    $1,054.322
//       
//          E:                     1.054322E+003
//          E0:                    1E+003
//          E1:                    1.1E+003
//          E2:                    1.05E+003
//          E3:                    1.054E+003
//       
//          e:                     1.054322e+003
//          e0:                    1e+003
//          e1:                    1.1e+003
//          e2:                    1.05e+003
//          e3:                    1.054e+003
//       
//          F:                     1054.32
//          F0:                    1054
//          F1:                    1054.3
//          F2:                    1054.32
//          F3:                    1054.322
//       
//          G:                     1054.32179
//          G0:                    1054.32179
//          G1:                    1E+03
//          G2:                    1.1E+03
//          G3:                    1.05E+03
//       
//          N:                     1,054.32
//          N0:                    1,054
//          N1:                    1,054.3
//          N2:                    1,054.32
//          N3:                    1,054.322
//       
//          P:                     105,432.18 %
//          P0:                    105,432 %
//          P1:                    105,432.2 %
//          P2:                    105,432.18 %
//          P3:                    105,432.179 %
//       
//          R:                     1054.32179
//          #,000.000:             1,054.322
//          0.###E-000:            1.054E003
//          000,000,000,000.00###: 000,000,001,054.32179
//       
//       Formatting of -195489100.8377:
//          C:                     ($195,489,100.84)
//          C0:                    ($195,489,101)
//          C1:                    ($195,489,100.8)
//          C2:                    ($195,489,100.84)
//          C3:                    ($195,489,100.838)
//       
//          E:                     -1.954891E+008
//          E0:                    -2E+008
//          E1:                    -2.0E+008
//          E2:                    -1.95E+008
//          E3:                    -1.955E+008
//       
//          e:                     -1.954891e+008
//          e0:                    -2e+008
//          e1:                    -2.0e+008
//          e2:                    -1.95e+008
//          e3:                    -1.955e+008
//       
//          F:                     -195489100.84
//          F0:                    -195489101
//          F1:                    -195489100.8
//          F2:                    -195489100.84
//          F3:                    -195489100.838
//       
//          G:                     -195489100.8377
//          G0:                    -195489100.8377
//          G1:                    -2E+08
//          G2:                    -2E+08
//          G3:                    -1.95E+08
//
//          N:                     -195,489,100.84
//          N0:                    -195,489,101
//          N1:                    -195,489,100.8
//          N2:                    -195,489,100.84
//          N3:                    -195,489,100.838
//
//          P:                     -19,548,910,083.77 %
//          P0:                    -19,548,910,084 %
//          P1:                    -19,548,910,083.8 %
//          P2:                    -19,548,910,083.77 %
//          P3:                    -19,548,910,083.770 %
//
//          R:                     -195489100.8377
//          #,000.000:             -195,489,100.838
//          0.###E-000:            -1.955E008
//          000,000,000,000.00###: -000,195,489,100.8377
//
//       Formatting of 1.0437E+21:
//          C:                     $1,043,700,000,000,000,000,000.00
//          C0:                    $1,043,700,000,000,000,000,000
//          C1:                    $1,043,700,000,000,000,000,000.0
//          C2:                    $1,043,700,000,000,000,000,000.00
//          C3:                    $1,043,700,000,000,000,000,000.000
//
//          E:                     1.043700E+021
//          E0:                    1E+021
//          E1:                    1.0E+021
//          E2:                    1.04E+021
//          E3:                    1.044E+021
//
//          e:                     1.043700e+021
//          e0:                    1e+021
//          e1:                    1.0e+021
//          e2:                    1.04e+021
//          e3:                    1.044e+021
//
//          F:                     1043700000000000000000.00
//          F0:                    1043700000000000000000
//          F1:                    1043700000000000000000.0
//          F2:                    1043700000000000000000.00
//          F3:                    1043700000000000000000.000
//
//          G:                     1.0437E+21
//          G0:                    1.0437E+21
//          G1:                    1E+21
//          G2:                    1E+21
//          G3:                    1.04E+21
//
//          N:                     1,043,700,000,000,000,000,000.00
//          N0:                    1,043,700,000,000,000,000,000
//          N1:                    1,043,700,000,000,000,000,000.0
//          N2:                    1,043,700,000,000,000,000,000.00
//          N3:                    1,043,700,000,000,000,000,000.000
//
//          P:                     104,370,000,000,000,000,000,000.00 %
//          P0:                    104,370,000,000,000,000,000,000 %
//          P1:                    104,370,000,000,000,000,000,000.0 %
//          P2:                    104,370,000,000,000,000,000,000.00 %
//          P3:                    104,370,000,000,000,000,000,000.000 %
//
//          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
//          C0:                    $0
//          C1:                    $0.0
//          C2:                    $0.00
//          C3:                    $0.000
//
//          E:                     -1.057300E-005
//          E0:                    -1E-005
//          E1:                    -1.1E-005
//          E2:                    -1.06E-005
//          E3:                    -1.057E-005
//
//          e:                     -1.057300e-005
//          e0:                    -1e-005
//          e1:                    -1.1e-005
//          e2:                    -1.06e-005
//          e3:                    -1.057e-005
//
//          F:                     0.00
//          F0:                    0
//          F1:                    0.0
//          F2:                    0.00
//          F3:                    0.000
//
//          G:                     -1.0573E-05
//          G0:                    -1.0573E-05
//          G1:                    -1E-05
//          G2:                    -1.1E-05
//          G3:                    -1.06E-05
//
//          N:                     0.00
//          N0:                    0
//          N1:                    0.0
//          N2:                    0.00
//          N3:                    0.000
//
//          P:                     0.00 %
//          P0:                    0 %
//          P1:                    0.0 %
//          P2:                    0.00 %
//          P3:                    -0.001 %
//
//          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)
{
   Console.WriteLine("Formatting of {0}:", number);
   foreach (string specifier in specifiers) {
      Console.WriteLine("   {0,-22} {1}", 
                        specifier + ":", number.ToString(specifier));
      // Add precision specifiers from 0 to 3.
      if (specifier.Length == 1 & ! specifier.Equals("R")) {
         for (int precision = 0; precision <= 3; precision++) {
            string pSpecifier = String.Format("{0}{1}", specifier, precision);
            Console.WriteLine("   {0,-22} {1}", 
                              pSpecifier + ":", number.ToString(pSpecifier));
         }    
         Console.WriteLine();
      }   
   }
   Console.WriteLine();
}
// The example displays the following output to the console:
//       Formatting of 1054.32179:
//          C:                     $1,054.32
//          C0:                    $1,054
//          C1:                    $1,054.3
//          C2:                    $1,054.32
//          C3:                    $1,054.322
//       
//          E:                     1.054322E+003
//          E0:                    1E+003
//          E1:                    1.1E+003
//          E2:                    1.05E+003
//          E3:                    1.054E+003
//       
//          e:                     1.054322e+003
//          e0:                    1e+003
//          e1:                    1.1e+003
//          e2:                    1.05e+003
//          e3:                    1.054e+003
//       
//          F:                     1054.32
//          F0:                    1054
//          F1:                    1054.3
//          F2:                    1054.32
//          F3:                    1054.322
//       
//          G:                     1054.32179
//          G0:                    1054.32179
//          G1:                    1E+03
//          G2:                    1.1E+03
//          G3:                    1.05E+03
//       
//          N:                     1,054.32
//          N0:                    1,054
//          N1:                    1,054.3
//          N2:                    1,054.32
//          N3:                    1,054.322
//       
//          P:                     105,432.18 %
//          P0:                    105,432 %
//          P1:                    105,432.2 %
//          P2:                    105,432.18 %
//          P3:                    105,432.179 %
//       
//          R:                     1054.32179
//          #,000.000:             1,054.322
//          0.###E-000:            1.054E003
//          000,000,000,000.00###: 000,000,001,054.32179
//       
//       Formatting of -195489100.8377:
//          C:                     ($195,489,100.84)
//          C0:                    ($195,489,101)
//          C1:                    ($195,489,100.8)
//          C2:                    ($195,489,100.84)
//          C3:                    ($195,489,100.838)
//       
//          E:                     -1.954891E+008
//          E0:                    -2E+008
//          E1:                    -2.0E+008
//          E2:                    -1.95E+008
//          E3:                    -1.955E+008
//       
//          e:                     -1.954891e+008
//          e0:                    -2e+008
//          e1:                    -2.0e+008
//          e2:                    -1.95e+008
//          e3:                    -1.955e+008
//       
//          F:                     -195489100.84
//          F0:                    -195489101
//          F1:                    -195489100.8
//          F2:                    -195489100.84
//          F3:                    -195489100.838
//       
//          G:                     -195489100.8377
//          G0:                    -195489100.8377
//          G1:                    -2E+08
//          G2:                    -2E+08
//          G3:                    -1.95E+08
//       
//          N:                     -195,489,100.84
//          N0:                    -195,489,101
//          N1:                    -195,489,100.8
//          N2:                    -195,489,100.84
//          N3:                    -195,489,100.838
//       
//          P:                     -19,548,910,083.77 %
//          P0:                    -19,548,910,084 %
//          P1:                    -19,548,910,083.8 %
//          P2:                    -19,548,910,083.77 %
//          P3:                    -19,548,910,083.770 %
//       
//          R:                     -195489100.8377
//          #,000.000:             -195,489,100.838
//          0.###E-000:            -1.955E008
//          000,000,000,000.00###: -000,195,489,100.8377
//       
//       Formatting of 1.0437E+21:
//          C:                     $1,043,700,000,000,000,000,000.00
//          C0:                    $1,043,700,000,000,000,000,000
//          C1:                    $1,043,700,000,000,000,000,000.0
//          C2:                    $1,043,700,000,000,000,000,000.00
//          C3:                    $1,043,700,000,000,000,000,000.000
//       
//          E:                     1.043700E+021
//          E0:                    1E+021
//          E1:                    1.0E+021
//          E2:                    1.04E+021
//          E3:                    1.044E+021
//       
//          e:                     1.043700e+021
//          e0:                    1e+021
//          e1:                    1.0e+021
//          e2:                    1.04e+021
//          e3:                    1.044e+021
//       
//          F:                     1043700000000000000000.00
//          F0:                    1043700000000000000000
//          F1:                    1043700000000000000000.0
//          F2:                    1043700000000000000000.00
//          F3:                    1043700000000000000000.000
//       
//          G:                     1.0437E+21
//          G0:                    1.0437E+21
//          G1:                    1E+21
//          G2:                    1E+21
//          G3:                    1.04E+21
//       
//          N:                     1,043,700,000,000,000,000,000.00
//          N0:                    1,043,700,000,000,000,000,000
//          N1:                    1,043,700,000,000,000,000,000.0
//          N2:                    1,043,700,000,000,000,000,000.00
//          N3:                    1,043,700,000,000,000,000,000.000
//       
//          P:                     104,370,000,000,000,000,000,000.00 %
//          P0:                    104,370,000,000,000,000,000,000 %
//          P1:                    104,370,000,000,000,000,000,000.0 %
//          P2:                    104,370,000,000,000,000,000,000.00 %
//          P3:                    104,370,000,000,000,000,000,000.000 %
//       
//          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
//          C0:                    $0
//          C1:                    $0.0
//          C2:                    $0.00
//          C3:                    $0.000
//       
//          E:                     -1.057300E-005
//          E0:                    -1E-005
//          E1:                    -1.1E-005
//          E2:                    -1.06E-005
//          E3:                    -1.057E-005
//       
//          e:                     -1.057300e-005
//          e0:                    -1e-005
//          e1:                    -1.1e-005
//          e2:                    -1.06e-005
//          e3:                    -1.057e-005
//       
//          F:                     0.00
//          F0:                    0
//          F1:                    0.0
//          F2:                    0.00
//          F3:                    0.000
//       
//          G:                     -1.0573E-05
//          G0:                    -1.0573E-05
//          G1:                    -1E-05
//          G2:                    -1.1E-05
//          G3:                    -1.06E-05
//       
//          N:                     0.00
//          N0:                    0
//          N1:                    0.0
//          N2:                    0.00
//          N3:                    0.000
//       
//          P:                     0.00 %
//          P0:                    0 %
//          P1:                    0.0 %
//          P2:                    0.00 %
//          P3:                    -0.001 %
//       
//          R:                     -1.0573E-05
//          #,000.000:             000.000
//          0.###E-000:            -1.057E-005
//          000,000,000,000.00###: -000,000,000,000.00001 
Dim numbers() As Double = {1054.32179, -195489100.8377, 1.0437E21, _
                         -1.0573e-05}
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
   Console.WriteLine("Formatting of {0}:", number)
   For Each specifier As String In specifiers
      Console.WriteLine("   {0,-22} {1}", 
                        specifier + ":", number.ToString(specifier))
      ' Add precision specifiers from 0 to 3.
      If specifier.Length = 1 And Not specifier.Equals("R") Then
         For precision As Integer = 0 To 3
            Dim pSpecifier As String = String.Format("{0}{1}", specifier, precision)
            Console.WriteLine("   {0,-22} {1}", 
                              pSpecifier + ":", number.ToString(pSpecifier))
         Next
         Console.WriteLine()      
      End If
   Next
   Console.WriteLine()
Next
' The example displays the following output:
'       Formatting of 1054.32179:
'          C:                     $1,054.32
'          C0:                    $1,054
'          C1:                    $1,054.3
'          C2:                    $1,054.32
'          C3:                    $1,054.322
'       
'          E:                     1.054322E+003
'          E0:                    1E+003
'          E1:                    1.1E+003
'          E2:                    1.05E+003
'          E3:                    1.054E+003
'       
'          e:                     1.054322e+003
'          e0:                    1e+003
'          e1:                    1.1e+003
'          e2:                    1.05e+003
'          e3:                    1.054e+003
'       
'          F:                     1054.32
'          F0:                    1054
'          F1:                    1054.3
'          F2:                    1054.32
'          F3:                    1054.322
'       
'          G:                     1054.32179
'          G0:                    1054.32179
'          G1:                    1E+03
'          G2:                    1.1E+03
'          G3:                    1.05E+03
'       
'          N:                     1,054.32
'          N0:                    1,054
'          N1:                    1,054.3
'          N2:                    1,054.32
'          N3:                    1,054.322
'       
'          P:                     105,432.18 %
'          P0:                    105,432 %
'          P1:                    105,432.2 %
'          P2:                    105,432.18 %
'          P3:                    105,432.179 %
'       
'          R:                     1054.32179
'          #,000.000:             1,054.322
'          0.###E-000:            1.054E003
'          000,000,000,000.00###: 000,000,001,054.32179
'       
'       Formatting of -195489100.8377:
'          C:                     ($195,489,100.84)
'          C0:                    ($195,489,101)
'          C1:                    ($195,489,100.8)
'          C2:                    ($195,489,100.84)
'          C3:                    ($195,489,100.838)
'       
'          E:                     -1.954891E+008
'          E0:                    -2E+008
'          E1:                    -2.0E+008
'          E2:                    -1.95E+008
'          E3:                    -1.955E+008
'       
'          e:                     -1.954891e+008
'          e0:                    -2e+008
'          e1:                    -2.0e+008
'          e2:                    -1.95e+008
'          e3:                    -1.955e+008
'       
'          F:                     -195489100.84
'          F0:                    -195489101
'          F1:                    -195489100.8
'          F2:                    -195489100.84
'          F3:                    -195489100.838
'       
'          G:                     -195489100.8377
'          G0:                    -195489100.8377
'          G1:                    -2E+08
'          G2:                    -2E+08
'          G3:                    -1.95E+08
'       
'          N:                     -195,489,100.84
'          N0:                    -195,489,101
'          N1:                    -195,489,100.8
'          N2:                    -195,489,100.84
'          N3:                    -195,489,100.838
'       
'          P:                     -19,548,910,083.77 %
'          P0:                    -19,548,910,084 %
'          P1:                    -19,548,910,083.8 %
'          P2:                    -19,548,910,083.77 %
'          P3:                    -19,548,910,083.770 %
'       
'          R:                     -195489100.8377
'          #,000.000:             -195,489,100.838
'          0.###E-000:            -1.955E008
'          000,000,000,000.00###: -000,195,489,100.8377
'       
'       Formatting of 1.0437E+21:
'          C:                     $1,043,700,000,000,000,000,000.00
'          C0:                    $1,043,700,000,000,000,000,000
'          C1:                    $1,043,700,000,000,000,000,000.0
'          C2:                    $1,043,700,000,000,000,000,000.00
'          C3:                    $1,043,700,000,000,000,000,000.000
'       
'          E:                     1.043700E+021
'          E0:                    1E+021
'          E1:                    1.0E+021
'          E2:                    1.04E+021
'          E3:                    1.044E+021
'       
'          e:                     1.043700e+021
'          e0:                    1e+021
'          e1:                    1.0e+021
'          e2:                    1.04e+021
'          e3:                    1.044e+021
'       
'          F:                     1043700000000000000000.00
'          F0:                    1043700000000000000000
'          F1:                    1043700000000000000000.0
'          F2:                    1043700000000000000000.00
'          F3:                    1043700000000000000000.000
'       
'          G:                     1.0437E+21
'          G0:                    1.0437E+21
'          G1:                    1E+21
'          G2:                    1E+21
'          G3:                    1.04E+21
'       
'          N:                     1,043,700,000,000,000,000,000.00
'          N0:                    1,043,700,000,000,000,000,000
'          N1:                    1,043,700,000,000,000,000,000.0
'          N2:                    1,043,700,000,000,000,000,000.00
'          N3:                    1,043,700,000,000,000,000,000.000
'       
'          P:                     104,370,000,000,000,000,000,000.00 %
'          P0:                    104,370,000,000,000,000,000,000 %
'          P1:                    104,370,000,000,000,000,000,000.0 %
'          P2:                    104,370,000,000,000,000,000,000.00 %
'          P3:                    104,370,000,000,000,000,000,000.000 %
'       
'          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
'          C0:                    $0
'          C1:                    $0.0
'          C2:                    $0.00
'          C3:                    $0.000
'       
'          E:                     -1.057300E-005
'          E0:                    -1E-005
'          E1:                    -1.1E-005
'          E2:                    -1.06E-005
'          E3:                    -1.057E-005
'       
'          e:                     -1.057300e-005
'          e0:                    -1e-005
'          e1:                    -1.1e-005
'          e2:                    -1.06e-005
'          e3:                    -1.057e-005
'       
'          F:                     0.00
'          F0:                    0
'          F1:                    0.0
'          F2:                    0.00
'          F3:                    0.000
'       
'          G:                     -1.0573E-05
'          G0:                    -1.0573E-05
'          G1:                    -1E-05
'          G2:                    -1.1E-05
'          G3:                    -1.06E-05
'       
'          N:                     0.00
'          N0:                    0
'          N1:                    0.0
'          N2:                    0.00
'          N3:                    0.000
'       
'          P:                     0.00 %
'          P0:                    0 %
'          P1:                    0.0 %
'          P2:                    0.00 %
'          P3:                    -0.001 %
'       
'          R:                     -1.0573E-05
'          #,000.000:             000.000
'          0.###E-000:            -1.057E-005
'          000,000,000,000.00###: -000,000,000,000.00001

備註

ToString(String)方法格式Double值中指定的格式,使用目前的文化特性的慣例。The ToString(String) method formats a Double value in a specified format by using the conventions of the current culture. 如果您想要指定不同的格式或文化特性,使用的其他多載ToString方法,如下所示: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
預設值 ("G") 格式Default ("G") format 預設值 (目前) 的文化特性Default (current) culture ToString()
預設值 ("G") 格式Default ("G") format 特定文化特性A specific culture ToString(IFormatProvider)
特定的格式或有效位數A specific format or precision 特定文化特性A specific culture ToString(String, IFormatProvider)

傳回值可以是PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbol,或數字,所指定的字串表示formatThe return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or the string representation of a number, as specified by format.

format參數可以是任何有效的標準數值格式規範,除了 D 和 X,以及自訂數值格式規範的任何組合。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. 如果formatnull或空字串,傳回的值會格式化為有一般的數值格式規範 ("G")。If format is null or an empty string, the return value is formatted with the general numeric format specifier ("G").

.NET Framework 會提供廣泛的格式化支援,這會在下列格式化主題中的更詳細地說明:The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:

根據預設,傳回的值只包含 15 個位數的精確度雖然內部維護最多 17 個位數。By default, the return value only contains 15 digits of precision although a maximum of 17 digits is maintained internally. 如果這個執行個體的值必須大於 15 位數,ToString會傳回PositiveInfinitySymbolNegativeInfinitySymbol而不是預期的數目。If the value of this instance has greater than 15 digits, ToString returns PositiveInfinitySymbol or NegativeInfinitySymbol instead of the expected number. 如果您需要更多有效位數時,指定format"G17"格式規格,它一定會傳回 17 個位數的有效位數或"R",它會傳回 15 位數如果數目可以使用 17 位數來表示,如果只能是數字表示使用最大有效位數。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.

給呼叫者的注意事項

在某些情況下,使用 "R" 標準數值格式字串格式化的 Double 值,如果使用 /platform:x64/platform:anycpu 參數編譯並在 64 位元系統上執行,則不會成功地反覆存取。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. 若要解決這個問題,您可以使用 "G17" 標準數值格式字串來格式化 Double 值。To work around this problem, you can format Double values by using the "G17" standard numeric format string. 下列範例使用 "R" 格式字串,搭配不會成功反覆存取的 Double 值,並且也會使用 "G17" 格式字串來成功反覆存取原始值。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.

[!code-csharpSystem.Double.ToString#6] [!code-vbSystem.Double.ToString#6][!code-csharpSystem.Double.ToString#6] [!code-vbSystem.Double.ToString#6]

另請參閱

ToString(IFormatProvider) ToString(IFormatProvider) ToString(IFormatProvider) ToString(IFormatProvider)

使用指定的特定文化特性格式資訊,將這個執行個體的數值轉換成它的相等字串表示。Converts the numeric value of this instance to its equivalent string representation using the specified culture-specific format information.

public:
 virtual System::String ^ ToString(IFormatProvider ^ provider);
public string ToString (IFormatProvider provider);
override this.ToString : IFormatProvider -> string
Public Function ToString (provider As IFormatProvider) As String

參數

provider
IFormatProvider IFormatProvider IFormatProvider IFormatProvider

物件,提供特定文化特性格式資訊。An object that supplies culture-specific formatting information.

傳回

這個執行個體值的字串表示,如同 provider 所指定。The string representation of the value of this instance as specified by provider.

實作

範例

下列範例顯示兩個的字串表示法Double值使用CultureInfo代表數個不同的文化特性的物件。The following example displays the string representation of two Double values using CultureInfo objects that represent several different cultures.

double value;

value = -16325.62015;
// Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture));
// Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")));
// Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")));

value = 16034.125E21;
// Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture));
// Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")));
// Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")));
// This example displays the following output to the console:
//       -16325.62015
//       -16325.62015
//       -16325,62015
//       1.6034125E+25
//       1.6034125E+25
//       1,6034125E+25
Dim value As Double 

value = -16325.62015
' Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture))
' Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")))
' Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")))

value = 16034.125E21
' Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture))
' Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")))
' Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")))
' This example displays the following output to the console:
'       -16325.62015
'       -16325.62015
'       -16325,62015
'       1.6034125E+25
'       1.6034125E+25
'       1,6034125E+25

下列範例示範如何將ToString、 正在接受StringIFormatProvider做為參數。The following example illustrates the use of ToString, taking a String and an IFormatProvider as parameters.

public ref class Temperature: public IFormattable
{
   // IFormattable.ToString implementation.
public:
   virtual String^ ToString( String^ format, IFormatProvider^ provider )
   {
      if ( format != nullptr )
      {
         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 );
   }

protected:
   // The value holder
   double m_value;

public:
   property double Value 
   {
      double get()
      {
         return m_value;
      }
      void set( double value )
      {
         m_value = value;
      }
   }

   property double Celsius 
   {
      double get()
      {
         return (m_value - 32.0) / 1.8;
      }
      void set( double value )
      {
         m_value = 1.8 * value + 32.0;
      }
   }
};
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;
		}
	}
}
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

備註

ToString(IFormatProvider)方法格式Double值預設值 ("G"或一般) 中指定的文化特性的格式。The ToString(IFormatProvider) method formats a Double value in the default ("G", or general) format of a specified culture. 如果您想要指定不同的格式或文化特性,使用的其他多載ToString方法,如下所示: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
預設值 ("G") 格式Default ("G") format 預設值 (目前)Default (current) ToString()
特定的格式或有效位數A specific format or precision 預設值 (目前) 的文化特性Default (current) culture ToString(String)
特定的格式或有效位數A specific format or precision 特定文化特性A specific culture ToString(String, IFormatProvider)

傳回值可以是PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbol,或格式的字串:The return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or a string of the form:

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

選擇性的項目包覆在方括號 ([和])。Optional elements are framed in square brackets ([ and ]). 包含詞彙 「 數字 」 的項目是由一系列的範圍從 0 到 9 的數字字元所組成。Elements that contain the term "digits" 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-digitsintegral-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-digitsfractional-digits 指定數字之小數點後的數字。A series of digits specifying the fractional part of the number.
'e''e' 小寫字元 'e',表示指數 (科學記號) 標記法。A lowercase character 'e', indicating exponential (scientific) notation.
exponential-digitsexponential-digits 一系列指定指數的數字。A series of digits specifying an exponent.

傳回值的一些範例包括"100"、"-123,456,789","123.45 e + 6"、"500"、"3.1416"、"600"、"-0.123"和"-Infinity"。Some examples of the return value are "100", "-123,456,789", "123.45e+6", "500", "3.1416", "600", "-0.123", and "-Infinity".

這個執行個體是以一般的數值格式規範 ("G") 格式。This instance is formatted with the general numeric format specifier ("G").

.NET Framework 會提供廣泛的格式化支援,這會在下列格式化主題中的更詳細地說明:The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:

provider參數是IFormatProvider實作其GetFormat方法會傳回NumberFormatInfo物件。The provider parameter is an IFormatProvider implementation whose GetFormat method returns a NumberFormatInfo object. 通常providerCultureInfo物件或NumberFormatInfo物件。Typically, provider is a CultureInfo object or a NumberFormatInfo object. provider參數提供格式化使用的特定文化特性資訊。The provider parameter supplies culture-specific information used in formatting. 如果providernull,傳回值的格式使用NumberFormatInfo目前的文化特性的物件。If provider is null, the return value is formatted using the NumberFormatInfo object for the current culture.

另請參閱

ToString() ToString() ToString() ToString()

將這個執行個體的數值轉換為它的相等字串表示。Converts the numeric value of this instance to its equivalent string representation.

public:
 override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String

傳回

這個執行個體值的字串表示。The string representation of the value of this instance.

範例

下列範例會使用預設值Double.ToString()方法,以顯示的數字的字串表示Double值。The following example uses the default Double.ToString() method to display the string representations of a number of Double values.

double number;

number = 1.6E20;
// Displays 1.6E+20.
Console.WriteLine(number.ToString());

number = 1.6E2;
// Displays 160.
Console.WriteLine(number.ToString());

number = -3.541;
// Displays -3.541.
Console.WriteLine(number.ToString());

number = -1502345222199E-07;
// Displays -150234.5222199.
Console.WriteLine(number.ToString());

number = -15023452221990199574E-09;
// Displays -15023452221.9902.
Console.WriteLine(number.ToString());

number = .60344;
// Displays 0.60344.
Console.WriteLine(number.ToString());

number = .000000001;
// Displays 1E-09.
Console.WriteLine(number.ToString());
Dim number As Double

number = 1.6E20
' Displays 1.6E+20.      
Console.WriteLIne(number.ToString())

number = 1.6E2
' Displays 160.
Console.WriteLine(number.ToString())

number = -3.541
' Displays -3.541.
Console.WriteLine(number.ToString())

number = -1502345222199E-07
' Displays -150234.5222199.
Console.WriteLine(number.ToString())

number = -15023452221990199574E-09
' Displays -15023452221.9902.
Console.WriteLine(number.ToString())

number = .60344
' Displays 0.60344.
Console.WriteLine(number.ToString())

number = .000000001
' Displays 1E-09.
Console.WriteLine(number.ToString())

下列範例示範如何將ToStringThe following example illustrates the use of ToString.

bool done = false;
String^ inp;
do
{
   Console::Write( "Enter a real number: " );
   inp = Console::ReadLine();
   try
   {
      d = Double::Parse( inp );
      Console::WriteLine( "You entered {0}.", d );
      done = true;
   }
   catch ( FormatException^ ) 
   {
      Console::WriteLine( "You did not enter a number." );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "An exception occurred while parsing your response: {0}", e );
   }

}
while (  !done );
   bool done = false;
   string inp;
   do {
      Console.Write("Enter a real number: ");
      inp = Console.ReadLine();
      try {
         d = Double.Parse(inp);
         Console.WriteLine("You entered {0}.", d.ToString());
         done = true;
      } 
      catch (FormatException) {
         Console.WriteLine("You did not enter a number.");
      }
catch (ArgumentNullException) {
         Console.WriteLine("You did not supply any input.");
      }
      catch (OverflowException) {
          Console.WriteLine("The value you entered, {0}, is out of range.", inp);      
      }
   } while (!done);
Dim Done As Boolean = False
Dim Inp As String
Do

    Console.Write("Enter a real number: ")
    inp = Console.ReadLine()
    Try
        D = Double.Parse(inp)
        Console.WriteLine("You entered " + D.ToString() + ".")
        Done = True
    Catch e As FormatException
        Console.WriteLine("You did not enter a number.")
    Catch e As ArgumentNullException
       Console.WriteLine("You did not supply any input.")
    Catch e As OverflowException
       Console.WriteLine("The value you entered, {0}, is out of range.", inp)      
    End Try
Loop While Not Done

備註

ToString()方法格式Double值預設值 ("G"或一般) 目前的文化特性的格式。The ToString() method formats a Double value in the default ("G", or general) format of the current culture. 如果您想要指定不同的格式、 有效位數或文化特性,使用的其他多載ToString方法,如下所示:If you want to specify a different format, precision, or culture, use the other overloads of the ToString method, as follows:

若要使用格式To use format 文化特性For culture 使用多載Use the overload
預設值 ("G") 格式Default ("G") format 特定文化特性A specific culture ToString(IFormatProvider)
特定的格式或有效位數A specific format or precision 預設值 (目前) 的文化特性Default (current) culture ToString(String)
特定的格式或有效位數A specific format or precision 特定文化特性A specific culture ToString(String, IFormatProvider)

傳回值可以是PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbol,或格式的字串:The return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or a string of the form:

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

選擇性的項目包覆在方括號 ([和])。Optional elements are framed in square brackets ([ and ]). 包含詞彙 「 數字 」 的項目是由一系列的範圍從 0 到 9 的數字字元所組成。Elements that contain the term "digits" 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-digitsintegral-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-digitsfractional-digits 指定數字之小數點後的數字。A series of digits specifying the fractional part of the number.
'e''e' 小寫字元 'e',表示指數 (科學記號) 標記法。A lowercase character 'e', indicating exponential (scientific) notation.
exponential-digitsexponential-digits 一系列指定指數的數字。A series of digits specifying an exponent.

傳回值的一些範例包括"100"、"-123,456,789","123.45 e + 6"、"500"、"3.1416"、"600"、"-0.123"和"-Infinity"。Some examples of the return value are "100", "-123,456,789", "123.45e+6", "500", "3.1416", "600", "-0.123", and "-Infinity".

.NET Framework 會提供廣泛的格式化支援,這會在下列格式化主題中的更詳細地說明:The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:

另請參閱

適用於