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.GetFormat 方法返回 NumberFormatInfo 对象的 IFormatProvider 实现。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 参数提供用于设置格式的区域性特定信息。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. 如果 format null 或空字符串,则此实例的返回值将使用通用数值格式说明符("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 对象的 IFormatProvider 实现。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 参数提供用于设置格式的区域性特定信息。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. 如果 format null 或空字符串,则此实例的返回值将使用通用数值格式说明符(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      

下面的示例显示帐户中的资金量。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:

另请参阅

适用于