使用指定的格式和区域性特定格式信息,将此实例的数值转换为它的等效字符串表示形式。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
标准或自定义的数值格式字符串。A standard or custom numeric format string.
一个提供区域性特定的格式设置信息的对象。An object that supplies culture-specific formatting information.
返回
此实例的值的字符串表示形式,由 format 和 provider 指定。The string representation of the value of this instance as specified by format and provider.
实现
例外
format 无效。format is invalid.
示例
下面的示例 SByte 使用标准数值格式说明符和多个特定对象显示正值和负值 CultureInfo 。The following example displays both a positive and a negative SByte value using the standard numeric format specifiers and a number of specific CultureInfo objects.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
// Define cultures whose formatting conventions are to be used.
CultureInfo[] cultures = { CultureInfo.CreateSpecificCulture("en-US"),
CultureInfo.CreateSpecificCulture("fr-FR"),
CultureInfo.CreateSpecificCulture("es-ES") };
sbyte positiveNumber = 119;
sbyte negativeNumber = -45;
string[] specifiers = {"G", "C", "D4", "E2", "F", "N", "P", "X2"};
foreach (string specifier in specifiers)
{
foreach (CultureInfo culture in cultures)
Console.WriteLine("{0,2} 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: 119 -45
// G format using fr-FR culture: 119 -45
// G format using es-ES culture: 119 -45
//
// C format using en-US culture: $119.00 ($45.00)
// C format using fr-FR culture: 119,00 € -45,00 €
// C format using es-ES culture: 119,00 € -45,00 €
//
// D4 format using en-US culture: 0119 -0045
// D4 format using fr-FR culture: 0119 -0045
// D4 format using es-ES culture: 0119 -0045
//
// E2 format using en-US culture: 1.19E+002 -4.50E+001
// E2 format using fr-FR culture: 1,19E+002 -4,50E+001
// E2 format using es-ES culture: 1,19E+002 -4,50E+001
//
// F format using en-US culture: 119.00 -45.00
// F format using fr-FR culture: 119,00 -45,00
// F format using es-ES culture: 119,00 -45,00
//
// N format using en-US culture: 119.00 -45.00
// N format using fr-FR culture: 119,00 -45,00
// N format using es-ES culture: 119,00 -45,00
//
// P format using en-US culture: 11,900.00 % -4,500.00 %
// P format using fr-FR culture: 11 900,00 % -4 500,00 %
// P format using es-ES culture: 11.900,00 % -4.500,00 %
//
// X2 format using en-US culture: 77 D3
// X2 format using fr-FR culture: 77 D3
// X2 format using es-ES culture: 77 D3Imports System.Globalization
Module Example
Public Sub Main()
' 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 SByte = 119
Dim negativeNumber As SByte = -45
Dim specifiers() As String = {"G", "C", "D4", "E2", "F", "N", "P", "X2"}
For Each specifier As String In specifiers
For Each culture As CultureInfo In Cultures
Console.WriteLine("{0,2} format using {1} culture: {2, 16} {3, 16}", _
specifier, culture.Name, _
positiveNumber.ToString(specifier, culture), _
negativeNumber.ToString(specifier, culture))
Next
Console.WriteLine()
Next
End Sub
End Module
' The example displays the following output:
' G format using en-US culture: 119 -45
' G format using fr-FR culture: 119 -45
' G format using es-ES culture: 119 -45
'
' C format using en-US culture: $119.00 ($45.00)
' C format using fr-FR culture: 119,00 € -45,00 €
' C format using es-ES culture: 119,00 € -45,00 €
'
' D4 format using en-US culture: 0119 -0045
' D4 format using fr-FR culture: 0119 -0045
' D4 format using es-ES culture: 0119 -0045
'
' E2 format using en-US culture: 1.19E+002 -4.50E+001
' E2 format using fr-FR culture: 1,19E+002 -4,50E+001
' E2 format using es-ES culture: 1,19E+002 -4,50E+001
'
' F format using en-US culture: 119.00 -45.00
' F format using fr-FR culture: 119,00 -45,00
' F format using es-ES culture: 119,00 -45,00
'
' N format using en-US culture: 119.00 -45.00
' N format using fr-FR culture: 119,00 -45,00
' N format using es-ES culture: 119,00 -45,00
'
' P format using en-US culture: 11,900.00 % -4,500.00 %
' P format using fr-FR culture: 11 900,00 % -4 500,00 %
' P format using es-ES culture: 11.900,00 % -4.500,00 %
'
' X2 format using en-US culture: 77 D3
' X2 format using fr-FR culture: 77 D3
' X2 format using es-ES culture: 77 D3
注解
The ToString(String, IFormatProvider) method formats an SByte 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
默认 ( "G" ) 格式Default ("G") format
特定区域性A specific culture
特定格式A specific format
默认 (当前) 区域性Default (current) culture
format参数可以是任何有效的标准数值格式说明符,也可以是自定义数字格式说明符的任意组合。The format parameter can be any valid standard numeric format specifier, or any combination of custom numeric format specifiers. 如果 format 等于 String.Empty 或为,则 null 当前对象的返回值的格式 SByte 为常规格式说明符 ( "G" ) 。If format is equal to String.Empty or is null, the return value of the current SByte object is formatted with the general format specifier ("G"). 如果 format 是其他任何值,则该方法将引发 FormatException 。If format is any other value, the method throws a FormatException.
.NET Framework 提供了广泛的格式设置支持,以下格式设置主题更详细地介绍了这些支持:The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:
有关数字格式说明符的详细信息,请参阅 标准数字格式字符串 和 自定义数字格式字符串。For more information about numeric format specifiers, see Standard Numeric Format Strings and Custom Numeric Format Strings.
有关 .NET Framework 中的格式设置的详细信息,请参阅 格式设置类型。For more information about support for formatting in the .NET Framework, see Formatting Types.
provider参数是一个 IFormatProvider 实现。The provider parameter is an IFormatProvider implementation. 其 GetFormat 方法返回一个 NumberFormatInfo 对象,该对象提供有关此方法返回的字符串格式的区域性特定信息。Its GetFormat method returns a NumberFormatInfo object that provides culture-specific information about the format of the string returned by this method. When the ToString(String, IFormatProvider) method is invoked, it calls the provider parameter's IFormatProvider.GetFormat method and passes it a Type object that represents the NumberFormatInfo type. GetFormat然后,方法返回 NumberFormatInfo 对象,该对象提供用于设置参数格式的信息 value ,如负号符号、组分隔符符号或小数点符号。The GetFormat method then returns the NumberFormatInfo object that provides information for formatting the value parameter, such as the negative sign symbol, the group separator symbol, or the decimal point symbol. There are three ways to use the provider parameter to supply formatting information to the ToString(String, IFormatProvider) method:
可以传递一个 CultureInfo 对象,该对象表示提供格式设置信息的区域性。You can pass a CultureInfo object that represents the culture that supplies formatting information. 其 GetFormat 方法返回 NumberFormatInfo 对象,该对象提供该区域性的数字格式设置信息。Its GetFormat method returns the NumberFormatInfo object that provides numeric formatting information for that culture.
可以传递 NumberFormatInfo 提供数字格式设置信息的实际对象。You can pass the actual NumberFormatInfo object that provides numeric formatting information. (其实现 GetFormat 仅返回自身。 )(Its implementation of GetFormat just returns itself.)
You can pass a custom object that implements IFormatProvider. Its GetFormat method instantiates and returns the NumberFormatInfo object that provides formatting information.
如果 provider 为 null ,则返回的字符串的格式基于 NumberFormatInfo 当前区域性的对象。If provider is null, the formatting of the returned string is based on the NumberFormatInfo object of the current culture.
另请参阅
适用于