Int32.ToString 方法

定義

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

多載

ToString(IFormatProvider)

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

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)

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

ToString()

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

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:
 System::String ^ ToString(IFormatProvider ^ provider);
public string ToString (IFormatProvider provider);
public string ToString (IFormatProvider? provider);
override this.ToString : IFormatProvider -> string
Public Function ToString (provider As IFormatProvider) As String

參數

provider
IFormatProvider

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

傳回

String

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

實作

範例

下列範例 Int32 CultureInfo 會使用代表數個不同文化特性的物件,來顯示值的字串表示。The following example displays the string representation of an Int32 value using CultureInfo objects that represent several different cultures.

using namespace System;
using namespace System::Globalization;

void main()
{
    int value = -16325;
    // 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")));
}
// The example displays the following output:
//       -16325
//       -16325
//       -16325
int value = -16325;
// 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
//       -16325
//       -16325
Dim value As Integer = -16325
' 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
'       -16325
'       -16325

備註

ToString(IFormatProvider)方法會 Int32 使用指定文化特性的物件,將預設 ( "G" 或一般) 格式的值格式化 NumberFormatInfoThe ToString(IFormatProvider) method formats an Int32 value in the default ("G", or general) format by using the NumberFormatInfo object of a specified culture. 如果您想要指定不同的格式或目前的文化特性,請使用方法的其他多載,如下所示 ToStringIf you want to specify a different format or the current 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()
特定格式A specific format 預設 (目前) 文化特性Default (current) culture ToString(String)
特定格式A specific format 特定文化特性A specific culture ToString(String, IFormatProvider)

provider參數是實介面的物件 IFormatProviderThe provider parameter is an object that implements the IFormatProvider interface. 它的 GetFormat 方法 NumberFormatInfo 會傳回物件,該物件會提供這個方法所傳回之字串格式的特定文化特性資訊。Its GetFormat method returns a NumberFormatInfo object that provides culture-specific information about the format of the string that is returned by this method. 執行的物件 IFormatProvider 可以是下列任何一項:The object that implements IFormatProvider can be any of the following:

如果 providernullNumberFormatInfo 無法從取得物件,則 provider 會使用 NumberFormatInfo 執行緒目前文化特性的物件來格式化傳回值。If provider is null or a NumberFormatInfo object cannot be obtained from provider, the return value is formatted using the NumberFormatInfo object for the thread current culture. 如需執行緒目前文化特性的詳細資訊,請參閱 Thread.CurrentCultureFor information about the thread current culture, see Thread.CurrentCulture.

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

另請參閱

適用於

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);
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

標準或自訂數值格式字串。A standard or custom numeric format string.

provider
IFormatProvider

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

傳回

String

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

實作

例外狀況

format 無效或不受支援。format is invalid or not supported.

範例

下列範例會針對三種不同的文化特性,使用每個支援的標準數值格式規範來顯示正值和負值。The following example displays a positive and a negative value using each of the supported standard numeric format specifiers for three different cultures.

using namespace System;
using namespace System::Globalization;

void main()
{
    // Define cultures whose formatting conventions are to be used.
    array<CultureInfo^>^ cultures = { CultureInfo::CreateSpecificCulture("en-US"), 
                                      CultureInfo::CreateSpecificCulture("fr-FR"), 
                                      CultureInfo::CreateSpecificCulture("es-ES") };
    int positiveNumber = 1679;
    int negativeNumber = -3045;
    array<String^>^ specifiers = {"G", "C", "D8", "E2", "F", "N", "P", "X8"}; 
    
    for each (String^ specifier in specifiers)
    {
       for each (CultureInfo^ culture in cultures)
       {
          // Display values with "G" format specifier.
          Console::WriteLine("{0} format using {1} culture: {2, 16} {3, 16}",  
                            specifier, culture->Name, 
                            positiveNumber.ToString(specifier, culture), 
                            negativeNumber.ToString(specifier, culture));
       }
       Console::WriteLine();
    }
}
// The example displays the following output:
//       G format using en-US culture:             1679            -3045
//       G format using fr-FR culture:             1679            -3045
//       G format using es-ES culture:             1679            -3045
//       
//       C format using en-US culture:        $1,679.00      ($3,045.00)
//       C format using fr-FR culture:       1 679,00 €      -3 045,00 €
//       C format using es-ES culture:       1.679,00 €      -3.045,00 €
//       
//       D8 format using en-US culture:         00001679        -00003045
//       D8 format using fr-FR culture:         00001679        -00003045
//       D8 format using es-ES culture:         00001679        -00003045
//       
//       E2 format using en-US culture:        1.68E+003       -3.05E+003
//       E2 format using fr-FR culture:        1,68E+003       -3,05E+003
//       E2 format using es-ES culture:        1,68E+003       -3,05E+003
//       
//       F format using en-US culture:          1679.00         -3045.00
//       F format using fr-FR culture:          1679,00         -3045,00
//       F format using es-ES culture:          1679,00         -3045,00
//       
//       N format using en-US culture:         1,679.00        -3,045.00
//       N format using fr-FR culture:         1 679,00        -3 045,00
//       N format using es-ES culture:         1.679,00        -3.045,00
//       
//       P format using en-US culture:     167,900.00 %    -304,500.00 %
//       P format using fr-FR culture:     167 900,00 %    -304 500,00 %
//       P format using es-ES culture:     167.900,00 %    -304.500,00 %
//       
//       X8 format using en-US culture:         0000068F         FFFFF41B
//       X8 format using fr-FR culture:         0000068F         FFFFF41B
//       X8 format using es-ES culture:         0000068F         FFFFF41B
// Define cultures whose formatting conventions are to be used.
CultureInfo[] cultures = {CultureInfo.CreateSpecificCulture("en-US"),
                          CultureInfo.CreateSpecificCulture("fr-FR"),
                          CultureInfo.CreateSpecificCulture("es-ES") };
int positiveNumber = 1679;
int negativeNumber = -3045;
string[] specifiers = {"G", "C", "D8", "E2", "F", "N", "P", "X8"};

foreach (string specifier in specifiers)
{
   foreach (CultureInfo culture in cultures)
   {
      // Display values with "G" format specifier.
      Console.WriteLine("{0} format using {1} culture: {2, 16} {3, 16}",
                        specifier, culture.Name,
                        positiveNumber.ToString(specifier, culture),
                        negativeNumber.ToString(specifier, culture));
   }
   Console.WriteLine();
}
// The example displays the following output:
//       G format using en-US culture:             1679            -3045
//       G format using fr-FR culture:             1679            -3045
//       G format using es-ES culture:             1679            -3045
//
//       C format using en-US culture:        $1,679.00      ($3,045.00)
//       C format using fr-FR culture:       1 679,00 €      -3 045,00 €
//       C format using es-ES culture:       1.679,00 €      -3.045,00 €
//
//       D8 format using en-US culture:         00001679        -00003045
//       D8 format using fr-FR culture:         00001679        -00003045
//       D8 format using es-ES culture:         00001679        -00003045
//
//       E2 format using en-US culture:        1.68E+003       -3.05E+003
//       E2 format using fr-FR culture:        1,68E+003       -3,05E+003
//       E2 format using es-ES culture:        1,68E+003       -3,05E+003
//
//       F format using en-US culture:          1679.00         -3045.00
//       F format using fr-FR culture:          1679,00         -3045,00
//       F format using es-ES culture:          1679,00         -3045,00
//
//       N format using en-US culture:         1,679.00        -3,045.00
//       N format using fr-FR culture:         1 679,00        -3 045,00
//       N format using es-ES culture:         1.679,00        -3.045,00
//
//       P format using en-US culture:     167,900.00 %    -304,500.00 %
//       P format using fr-FR culture:     167 900,00 %    -304 500,00 %
//       P format using es-ES culture:     167.900,00 %    -304.500,00 %
//
//       X8 format using en-US culture:         0000068F         FFFFF41B
//       X8 format using fr-FR culture:         0000068F         FFFFF41B
//       X8 format using es-ES culture:         0000068F         FFFFF41B
' Define cultures whose formatting conventions are to be used.
Dim cultures() As CultureInfo = {CultureInfo.CreateSpecificCulture("en-US"), _
                                 CultureInfo.CreateSpecificCulture("fr-FR"), _
                                 CultureInfo.CreateSpecificCulture("es-ES") }
Dim positiveNumber As Integer = 1679
Dim negativeNumber As Integer = -3045
Dim specifiers() As String = {"G", "C", "D8", "E2", "F", "N", "P", "X8"} 

For Each specifier As String In specifiers
   For Each culture As CultureInfo In Cultures
      ' Display values with "G" format specifier.
      Console.WriteLine("{0} format using {1} culture: {2, 16} {3, 16}", _ 
                        specifier, culture.Name, _
                        positiveNumber.ToString(specifier, culture), _
                        negativeNumber.ToString(specifier, culture))

   Next
   Console.WriteLine()
Next
' The example displays the following output to the console:
'       G format using en-US culture:             1679            -3045
'       G format using fr-FR culture:             1679            -3045
'       G format using es-ES culture:             1679            -3045
'       
'       C format using en-US culture:        $1,679.00      ($3,045.00)
'       C format using fr-FR culture:       1 679,00 €      -3 045,00 €
'       C format using es-ES culture:       1.679,00 €      -3.045,00 €
'       
'       D8 format using en-US culture:         00001679        -00003045
'       D8 format using fr-FR culture:         00001679        -00003045
'       D8 format using es-ES culture:         00001679        -00003045
'       
'       E2 format using en-US culture:        1.68E+003       -3.05E+003
'       E2 format using fr-FR culture:        1,68E+003       -3,05E+003
'       E2 format using es-ES culture:        1,68E+003       -3,05E+003
'       
'       F format using en-US culture:          1679.00         -3045.00
'       F format using fr-FR culture:          1679,00         -3045,00
'       F format using es-ES culture:          1679,00         -3045,00
'       
'       N format using en-US culture:         1,679.00        -3,045.00
'       N format using fr-FR culture:         1 679,00        -3 045,00
'       N format using es-ES culture:         1.679,00        -3.045,00
'       
'       P format using en-US culture:     167,900.00 %    -304,500.00 %
'       P format using fr-FR culture:     167 900,00 %    -304 500,00 %
'       P format using es-ES culture:     167.900,00 %    -304.500,00 %
'       
'       X8 format using en-US culture:         0000068F         FFFFF41B
'       X8 format using fr-FR culture:         0000068F         FFFFF41B
'       X8 format using es-ES culture:         0000068F         FFFFF41B

備註

ToString(String, IFormatProvider)方法會 Int32 使用指定之文化特性的物件,將指定格式的值格式化 NumberFormatInfoThe ToString(String, IFormatProvider) method formats an Int32 value in a specified format by using the NumberFormatInfo object of a specified culture. 如果您想要使用預設格式或文化特性設定,請使用方法的其他多載,如下所示 ToStringIf you want to use default format or culture settings, 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 預設 (目前) 文化特性Default (current) culture ToString(String)

format參數可以是標準或自訂數值格式字串。The format parameter can be either a standard or a custom numeric format string. 除了所有自訂數值格式字元以外,也支援 "R" (或 "r" ) 以外的所有標準數值格式字串。All standard numeric format strings other than "R" (or "r") are supported, as are all custom numeric format characters. 如果 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").

provider參數是實介面的物件 IFormatProviderThe provider parameter is an object that implements the IFormatProvider interface. 它的 GetFormat 方法 NumberFormatInfo 會傳回物件,該物件會提供這個方法所傳回之字串格式的特定文化特性格式資訊。Its GetFormat method returns a NumberFormatInfo object that provides culture-specific format information about the format of the string that is returned by this method. 執行的物件 IFormatProvider 可以是下列任何一項:The object that implements IFormatProvider can be any of the following:

如果 providernullNumberFormatInfo 無法從取得物件,則 provider 會使用目前文化特性的,將這個實例的傳回值格式化 NumberFormatInfoIf provider is null or a NumberFormatInfo object cannot be obtained from provider, the return value for this instance is formatted with the NumberFormatInfo for the current culture.

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

另請參閱

適用於

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);
public string ToString (string? format);
override this.ToString : string -> string
Public Function ToString (format As String) As String

參數

format
String

標準或自訂數值格式字串。A standard or custom numeric format string.

傳回

String

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

例外狀況

format 無效或不受支援。format is invalid or not supported.

範例

下列範例會 Int32 使用每個支援的標準數值格式規範來顯示值,以及兩個自訂數值格式字串。The following example displays an Int32 value using each of the supported standard numeric format specifiers, together with two custom numeric format strings. 在將數值轉換成字串時,此範例會使用 en-us 文化特性的格式化慣例。In converting the numeric values to strings, the example uses the formatting conventions of the en-US culture.

using namespace System;

void main()
{
    int value = -16325;
    String^ specifier;
    
    // Use standard numeric format specifiers.
    specifier = "G";
    Console::WriteLine("{0}: {1}", specifier, value.ToString(specifier));

    specifier = "C";
    Console::WriteLine("{0}: {1}", specifier, value.ToString(specifier));

    specifier = "D8";
    Console::WriteLine("{0}: {1}", specifier, value.ToString(specifier));

    specifier = "E4";
    Console::WriteLine("{0}: {1}", specifier, value.ToString(specifier));

    specifier = "e3";
    Console::WriteLine("{0}: {1}", specifier, value.ToString(specifier));

    specifier = "F";
    Console::WriteLine("{0}: {1}", specifier, value.ToString(specifier));

    specifier = "N";
    Console::WriteLine("{0}: {1}", specifier, value.ToString(specifier));

    specifier = "P";
    Console::WriteLine("{0}: {1}", specifier, (value/100000).ToString(specifier));

    specifier = "X";
    Console::WriteLine("{0}: {1}", specifier, value.ToString(specifier));
    
    // Use custom numeric format specifiers.
    specifier = "0,0.000";
    Console::WriteLine("{0}: {1}", specifier, value.ToString(specifier));

    specifier = "#,#.00#;(#,#.00#)";
    Console::WriteLine("{0}: {1}", specifier, (value*-1).ToString(specifier));
}   
// The example displays the following output:
//     G: -16325
//     C: ($16,325.00)
//     D8: -00016325
//     E4: -1.6325E+004
//     e3: -1.633e+004
//     F: -16325.00
//     N: -16,325.00
//     P: 0.00 %
//     X: FFFFC03B
//     0,0.000: -16,325.000
//     #,#.00#;(#,#.00#): 16,325.00
int value = -16325;
string specifier;

// Use standard numeric format specifier.
specifier = "G";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    G: -16325
specifier = "C";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    C: ($16,325.00)
specifier = "D8";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    D8: -00016325
specifier = "E4";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    E4: -1.6325E+004
specifier = "e3";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    e3: -1.633e+004
specifier = "F";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    F: -16325.00
specifier = "N";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    N: -16,325.00
specifier = "P";
Console.WriteLine("{0}: {1}", specifier, (value/100000).ToString(specifier));
// Displays:    P: -16.33 %
specifier = "X";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    X: FFFFC03B

// Use custom numeric format specifiers.
specifier = "0,0.000";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    0,0.000: -16,325.000
specifier = "#,#.00#;(#,#.00#)";
Console.WriteLine("{0}: {1}", specifier, (value*-1).ToString(specifier));
// Displays:    #,#.00#;(#,#.00#): 16,325.00
Dim value As Integer = -16325
Dim specifier As String

' Use standard numeric format specifier.
specifier = "G"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    G: -16325
specifier = "C"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    C: ($16,325.00)
specifier = "D8"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    D8: -00016325
specifier = "E4"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    E4: -1.6325E+004
specifier = "e3"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    e3: -1.633e+004
specifier = "F"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    F: -16325.00
specifier = "N"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    N: -16,325.00
specifier = "P"
Console.WriteLine("{0}: {1}", specifier, (value/100000).ToString(specifier))
' Displays:    P: -16.33 %
specifier = "X"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    X: FFFFC03B 

' Use custom numeric format specifiers.
specifier = "0,0.000"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    0,0.000: -16,325.000
specifier = "#,#.00#;(#,#.00#)"
Console.WriteLine("{0}: {1}", specifier, (value*-1).ToString(specifier))
' Displays:    #,#.00#;(#,#.00#): 16,325.00

備註

ToString(String)方法 Int32 NumberFormatInfo 會使用表示目前文化特性慣例的物件,將指定格式的值格式化。The ToString(String) method formats an Int32 value in a specified format by using a NumberFormatInfo object that represents the conventions of the current culture. 如果您想要使用預設 ( "G" 或一般) 格式,或指定不同的文化特性,請使用方法的其他多載,如下所示 ToStringIf you want to use the default ("G", or general) format or specify a different 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 特定文化特性A specific culture ToString(String, IFormatProvider)

format參數可以是任何有效的標準數值格式規範,除了 "R" 之外,還有任何自訂數值格式規範的組合。The format parameter can be any valid standard numeric format specifier except for "R", as well as any combination of custom numeric format specifiers. 如果 formatnull 或 ( "" ) 的空字串,則此實例的傳回值會以一般數值格式規範 ( "G" ) 來格式化。If format is null or an empty string (""), the return value of this instance is formatted with the general numeric format specifier ("G").

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

這個實例的傳回值會使用 NumberFormatInfo 目前文化特性的來格式化。The return value of this instance is formatted with the NumberFormatInfo for the current culture.

另請參閱

適用於

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

傳回

String

這個執行個體值的字串表示,包含減號 (如果數值為負) 及沒有零的前置字元且範圍從 0 到 9 的數字順序所組成。The string representation of the value of this instance, consisting of a negative sign if the value is negative, and a sequence of digits ranging from 0 to 9 with no leading zeroes.

範例

下列範例會 Int32 使用預設方法來顯示值 ToString()The following example displays an Int32 value using the default ToString() method. 它也會顯示 Int32 使用一些標準格式規範所產生之值的字串表示。It also displays the string representations of the Int32 value that results from using a number of standard format specifiers. 這些範例會使用 en-us 文化特性的格式化慣例來顯示。The examples are displayed using the formatting conventions of the en-US culture.

using namespace System;

void main()
{
    int value = -16325;
    // Display value using default ToString method.
    Console::WriteLine(value.ToString());             
    // Display value using some standard format specifiers.
    Console::WriteLine(value.ToString("G"));         
    Console::WriteLine(value.ToString("C"));         
    Console::WriteLine(value.ToString("D"));         
    Console::WriteLine(value.ToString("F"));         
    Console::WriteLine(value.ToString("N"));         
    Console::WriteLine(value.ToString("X"));              
}
// The example displays the following output:
//     -16325
//     -16325
//     ($16,325.00)
//     -16325
//     -16325.00
//     -16,325.00
//     FFFFC03B
int value = -16325;
// Display value using default ToString method.
Console.WriteLine(value.ToString());            // Displays -16325
// Display value using some standard format specifiers.
Console.WriteLine(value.ToString("G"));         // Displays -16325
Console.WriteLine(value.ToString("C"));         // Displays ($16,325.00)
Console.WriteLine(value.ToString("D"));         // Displays -16325
Console.WriteLine(value.ToString("F"));         // Displays -16325.00
Console.WriteLine(value.ToString("N"));         // Displays -16,325.00
Console.WriteLine(value.ToString("X"));         // Displays FFFFC03B
Dim value As Integer = -16325
' Display value using default ToString method.
Console.WriteLine(value.ToString())            ' Displays -16325
' Display value using some standard format specifiers.
Console.WriteLine(value.ToString("G"))         ' Displays -16325
Console.WriteLine(value.ToString("C"))         ' Displays ($16,325.00)
Console.WriteLine(value.ToString("D"))         ' Displays -16325
Console.WriteLine(value.ToString("F"))         ' Displays -16325.00
Console.WriteLine(value.ToString("N"))         ' Displays -16,325.00
Console.WriteLine(value.ToString("X"))         ' Displays FFFFC03B      

備註

ToString()方法會 Int32 使用目前文化特性的物件,將預設 ( "G" 或一般) 格式的值格式化 NumberFormatInfoThe ToString() method formats an Int32 value in the default ("G", or general) format by using the NumberFormatInfo object of the current culture. 如果您想要指定不同的格式或文化特性,請使用方法的其他多載,如下所示 ToStringIf 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 特定文化特性A specific culture ToString(IFormatProvider)
特定格式A specific format 預設 (目前) 文化特性Default (current) culture ToString(String)
特定格式A specific format 特定文化特性A specific culture ToString(String, IFormatProvider)

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

另請參閱

適用於