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

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

傳回

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

實作

範例

下列範例會使用代表數個不同文化特性的 CultureInfo 物件,顯示 Decimal 值的字串表示。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) 方法會使用指定文化特性的預設("G" 或一般)格式來格式化 Decimal 值。The ToString(IFormatProvider) method formats a Decimal value in the default ("G", or general) format of a specified culture. 如果您想要指定不同的格式或目前的文化特性,請使用 ToString 方法的其他多載,如下所示:If 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 Framework 提供廣泛的格式設定支援,在下列格式設定主題中有更詳細的說明:The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:

provider 參數是 IFormatProvider 的實作為,其 IFormatProvider.GetFormat 方法會傳回 NumberFormatInfo 物件。The 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 參數提供格式化時使用的文化特性(culture)特定資訊。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);
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.

傳回

這個執行個體值的字串表示,如同 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. 如果您想要使用預設格式或文化特性設定,請使用 ToString 方法的其他多載,如下所示:If 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 Framework 提供廣泛的格式設定支援,在下列格式設定主題中有更詳細的說明:The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:

provider 參數是 IFormatProvider 的實作為,其 IFormatProvider.GetFormat 方法會傳回 NumberFormatInfo 物件。The provider parameter is an IFormatProvider implementation whose IFormatProvider.GetFormat method returns a NumberFormatInfo object. 一般來說,providerNumberFormatInfoCultureInfo 物件。Typically, provider is a NumberFormatInfo or CultureInfo object. provider 參數提供格式化時使用的文化特性(culture)特定資訊。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);
override this.ToString : string -> string
Public Function ToString (format As String) As String

參數

format
String

標準或自訂數值格式字串。A standard or custom numeric format 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" 或一般)格式或指定不同的文化特性,請使用 ToString 方法的其他多載,如下所示:If 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 Framework 提供廣泛的格式設定支援,在下列格式設定主題中有更詳細的說明:The .NET Framework 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

傳回

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

範例

下列範例會使用預設的 ToString() 方法來顯示 Decimal 值。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      

下列範例會顯示帳戶中的 money 金額。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() 方法會以目前文化特性的預設("G" 或一般)格式來格式化 Decimal 值。The ToString() method formats a Decimal value in the default ("G", or general) format 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 特定文化特性A specific culture ToString(IFormatProvider)
特定格式A specific format 預設(目前)文化特性Default (current) culture ToString(String)
特定格式A specific format 特定文化特性A specific culture ToString(String, IFormatProvider)

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

另請參閱

適用於