Decimal.ToString 方法

定義

將這個執行個體的數值轉換成其對等的 String 表示。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.

實作

範例

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

decimal value = -16325.62m;
// 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.62
//       -16325.62
//       -16325,62
Dim value As Decimal = -16325.62d
' 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.62
'       -16325.62
'       -16325,62

備註

方法會將 ToString(IFormatProvider) Decimal 預設 ( "G" 或指定文化特性的一般) 格式的值格式化。The ToString(IFormatProvider) method formats a Decimal value in the default ("G", or general) format 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)

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

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

若要 Decimal 使用指定的文化特性和特定的格式字串,將值轉換成其字串表示,請呼叫 Decimal.ToString(String, IFormatProvider) 方法。To convert a Decimal value to its string representation using a specified culture and a specific format string, call the Decimal.ToString(String, IFormatProvider) method.

另請參閱

適用於

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 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.

範例

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

decimal value = 16325.62m;
string specifier;
CultureInfo culture;

// Use standard numeric format specifiers.
specifier = "G";
culture = CultureInfo.CreateSpecificCulture("eu-ES");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16325,62
Console.WriteLine(value.ToString(specifier, CultureInfo.InvariantCulture));
// Displays:    16325.62

specifier = "C";
culture = CultureInfo.CreateSpecificCulture("en-US");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    $16,325.62
culture = CultureInfo.CreateSpecificCulture("en-GB");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    £16,325.62

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,62
culture = CultureInfo.CreateSpecificCulture("en-CA");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16325.62

specifier = "N";
culture = CultureInfo.CreateSpecificCulture("es-ES");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16.325,62
culture = CultureInfo.CreateSpecificCulture("fr-CA");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16 325,62

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 Decimal = 16325.62d
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,62
Console.WriteLine(value.ToString(specifier, CultureInfo.InvariantCulture))
' Displays:    16325.62

specifier = "C"
culture = CultureInfo.CreateSpecificCulture("en-US")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    $16,325.62
culture = CultureInfo.CreateSpecificCulture("en-GB")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    £16,325.62

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,62
culture = CultureInfo.CreateSpecificCulture("en-CA")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    16325.62

specifier = "N"
culture = CultureInfo.CreateSpecificCulture("es-ES")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    16.325,62
culture = CultureInfo.CreateSpecificCulture("fr-CA")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    16 325,62

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(String, IFormatProvider) Decimal 之文化特性的指定格式來格式化值。The ToString(String, IFormatProvider) method formats a Decimal value in a specified format 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參數可以是任何有效的標準數值格式規範,除了 D、R 和 X 之外,還有任何自訂數值格式規範的組合。The format parameter can be any valid standard numeric format specifier except for D, R, 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 提供廣泛的格式化支援,在下列格式設定主題中有更詳細的說明:.NET provides extensive formatting support, which is described in greater detail in the following formatting topics:

provider參數是 IFormatProvider 其方法會傳回物件的實作為 IFormatProvider.GetFormat NumberFormatInfoThe provider parameter is an IFormatProvider implementation whose IFormatProvider.GetFormat method returns a NumberFormatInfo object. 通常 providerNumberFormatInfoCultureInfo 物件。Typically, provider is a NumberFormatInfo or CultureInfo object. provider參數會提供用於格式化的文化特性特定資訊。The provider parameter supplies culture-specific information used in formatting. 如果 providernull,則會使用執行緒目前的文化特性。If provider is null, the thread current culture is used.

另請參閱

適用於

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.

範例

下列範例會 Decimal 使用每個支援的標準數值格式規範來顯示值,以及兩個自訂數值格式字串。The following example displays a Decimal 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.

decimal value = 16325.62m;
string specifier;

// Use standard numeric format specifiers.
specifier = "G";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    G: 16325.62
specifier = "C";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    C: $16,325.62
specifier = "E04";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    E04: 1.6326E+004
specifier = "F";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    F: 16325.62
specifier = "N";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    N: 16,325.62
specifier = "P";
Console.WriteLine("{0}: {1}", specifier, (value/10000).ToString(specifier));
// Displays:    P: 163.26 %

// Use custom numeric format specifiers.
specifier = "0,0.000";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    0,0.000: 16,325.620
specifier = "#,#.00#;(#,#.00#)";
Console.WriteLine("{0}: {1}", specifier, (value*-1).ToString(specifier));
// Displays:    #,#.00#;(#,#.00#): (16,325.62)
Dim value As Decimal = 16325.62d
Dim specifier As String

' Use standard numeric format specifiers.
specifier = "G"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    G: 16325.62
specifier = "C"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    C: $16,325.62
specifier = "E04"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    E04: 1.6326E+004
specifier = "F"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    F: 16325.62
specifier = "N"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    N: 16,325.62
specifier = "P"
Console.WriteLine("{0}: {1}", specifier, (value/10000).ToString(specifier))
' Displays:    P: 163.26 %

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

備註

ToString(String)方法會 Decimal 使用目前文化特性的慣例,將指定格式的值格式化。The ToString(String) method formats a Decimal value in a specified format by using 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)

ToString方法會使用參數所指定的標準或自訂數值格式字串 format ,將目前實例的值轉換成其字串表示。The ToString method uses the standard or custom numeric format string specified by the format parameter to convert the value of the current instance into its string representation. format參數可以是任何有效的標準數值格式規範,除了 D、R 和 X 之外,還有任何自訂數值格式規範的組合。The format parameter can be any valid standard numeric format specifier except for D, R, 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 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 is formatted by using the NumberFormatInfo object for the current culture. 若要套用指定文化特性的格式化慣例,請呼叫 ToString(String, IFormatProvider) 方法。To apply the formatting conventions of a specified culture, call the ToString(String, IFormatProvider) method.

另請參閱

適用於

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

字串,表示這個執行個體的值。A string that represents the value of this instance.

範例

下列範例會 Decimal 使用預設方法來顯示值 ToString()The following example displays a Decimal value using the default ToString() method. 它也會顯示 Decimal 使用一些標準格式規範所產生之值的字串表示。It also displays the string representations of the Decimal value that result from using a number of standard format specifiers.

decimal value = -16325.62m;
// Display value using default ToString method.
Console.WriteLine(value.ToString());            // Displays -16325.62
// Display value using some standard format specifiers.
Console.WriteLine(value.ToString("G"));         // Displays -16325.62
Console.WriteLine(value.ToString("C"));         // Displays ($16,325.62)
Console.WriteLine(value.ToString("F"));         // Displays -16325.62
Dim value As Decimal = -16325.62d
' Display value using default ToString method.
Console.WriteLine(value.ToString())            ' Displays -16325.62
' Display value using some standard format specifiers.
Console.WriteLine(value.ToString("G"))         ' Displays -16325.62
Console.WriteLine(value.ToString("C"))         ' Displays ($16,325.62)
Console.WriteLine(value.ToString("F"))         ' Displays -16325.62      

下列範例會顯示帳戶中的金額。The following example displays the amount of money in an account.

   public ref class PiggyBank
   {
   public:
      void AddPenny()
      {
         MyFortune = Decimal::Add( MyFortune, (Decimal).01 );
      }

      virtual System::String^ ToString() override
      {
         return MyFortune.ToString("C")+" in piggy bank";
      }

   protected:
      Decimal MyFortune;
   };
}
class PiggyBank {
    public void AddPenny() {
        MyFortune = Decimal.Add(MyFortune, .01m);
    }

    public override string ToString() {
        return MyFortune.ToString("C")+" in piggy bank";
    }

    protected decimal MyFortune;
}
Class PiggyBank

    Public Sub AddPenny()
        MyFortune = [Decimal].Add(MyFortune, 0.01D)
    End Sub

    Public Overrides Function ToString() As String
        Return MyFortune.ToString("C") + " in piggy bank"
    End Function

    Protected MyFortune As Decimal
End Class

備註

方法會將 ToString() Decimal 預設 ( "G" 或目前文化特性的一般) 格式的值格式化。The ToString() method formats a Decimal value in the default ("G", or general) format 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:

另請參閱

適用於