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

一个 IFormatProvider,它提供区域性特定的格式设置信息。An IFormatProvider that supplies culture-specific formatting information.

返回

此实例的值的字符串表示形式,由 provider 指定。The string representation of the value of this instance as specified by provider.

实现

示例

下面的示例通过调用具有不同格式提供程序的 Int16.ToString(IFormatProvider) 方法来循环访问 Int16 值的数组,并将它们显示到控制台。The following example iterates an array of Int16 values and displays each of them to the console by calling the Int16.ToString(IFormatProvider) method with different format providers. 由于默认的 "G" 格式说明符定义了简单格式,因此无论 provider 参数的值是什么,为每个 Int16 值生成的格式化字符串都是相同的。Because of the simple formatting defined by the default "G" format specifier, the formatted strings produced for each Int16 value are identical regardless of the value of the provider parameter.

short[] numbers = {-23092, 0, 14894, Int16.MaxValue};
CultureInfo[] providers = {new CultureInfo("en-us"), 
                           new CultureInfo("fr-fr"), 
                           new CultureInfo("de-de"), 
                           new CultureInfo("es-es")};
foreach (Int16 int16Value in numbers)
{
   foreach (CultureInfo provider in providers)
   {
      Console.Write("{0, 6} ({1})     ", 
                    int16Value.ToString(provider), 
                    provider.Name);
   }
   Console.WriteLine();
}
// The example displays the following output to the console:
//       -23092 (en-US)     -23092 (fr-FR)     -23092 (de-DE)     -23092 (es-ES)
//            0 (en-US)          0 (fr-FR)          0 (de-DE)          0 (es-ES)
//        14894 (en-US)      14894 (fr-FR)      14894 (de-DE)      14894 (es-ES)
//        32767 (en-US)      32767 (fr-FR)      32767 (de-DE)      32767 (es-ES)      
Dim numbers() As Short = {-23092, 0, 14894, Int16.MaxValue}
Dim providers() As CultureInfo = {New CultureInfo("en-us"), _
                                  New CultureInfo("fr-fr"), _
                                  New CultureInfo("de-de"), _
                                  New CultureInfo("es-es")}
For Each int16Value As Short In Numbers
   For Each provider As CultureInfo In providers
      Console.Write("{0, 6} ({1})     ", _
                    int16Value.ToString(provider), _
                    provider.Name)
   Next                     
   Console.WriteLine()
Next 
' The example displays the following output to the console:
'       -23092 (en-US)     -23092 (fr-FR)     -23092 (de-DE)     -23092 (es-ES)
'            0 (en-US)          0 (fr-FR)          0 (de-DE)          0 (es-ES)
'        14894 (en-US)      14894 (fr-FR)      14894 (de-DE)      14894 (es-ES)
'        32767 (en-US)      32767 (fr-FR)      32767 (de-DE)      32767 (es-ES)      

注解

ToString(IFormatProvider) 方法通过使用指定区域性的 NumberFormatInfo 对象,设置默认值("G" 或 "常规")格式的 Int16 值的格式。The ToString(IFormatProvider) method formats an Int16 value in the default ("G", or general) format by using the NumberFormatInfo object 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. NumberFormatInfo 对象提供了有关此方法返回的字符串格式的区域性特定信息。The NumberFormatInfo object provides culture-specific information about the format of the string returned by this method. 如果 null``provider,则将此实例设置为当前区域性的 NumberFormatInfo 对象格式。If provider is null, this instance is formatted with the NumberFormatInfo object for the current culture.

另请参阅

ToString(String, IFormatProvider)

使用指定的格式和区域性特定格式设置信息,将此实例的数值转换为其等效字符串表示形式。Converts the numeric value of this instance to its equivalent string representation using the specified format and culture-specific formatting 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.

实现

示例

下面的示例使用四个不同区域性中的每个受支持的标准格式字符串来显示 Int16 值。The following example displays an Int16 value using each of the supported standard format strings in four different cultures.

Int16 value = 14603;
string[] formats = {"C", "D6", "e1", "E2", "F1", "G", "N1", 
                    "P0", "X4", "000000.0000", "##000.0"};
CultureInfo[] providers = {new CultureInfo("en-us"), 
                           new CultureInfo("fr-fr"), 
                           new CultureInfo("de-de"), 
                           new CultureInfo("es-es")};
// Display header.
Console.WriteLine("{0,24}{1,14}{2,14}{3,14}", providers[0], providers[1], 
                  providers[2], providers[3]);
Console.WriteLine();
// Display a value using each format string.
foreach (string format in formats)
{
   // Display the value for each provider on the same line.
   Console.Write("{0,-12}", format);
   foreach (CultureInfo provider in providers)
   {
      Console.Write("{0,12}  ", 
                    value.ToString(format, provider)); 
   }
   Console.WriteLine();
}
// The example displays the following output to the console:
//                       en-US         fr-FR         de-DE         es-ES
//    
//    C             $14,603.00   14 603,00 €   14.603,00 €   14.603,00 €  
//    D6                014603        014603        014603        014603  
//    e1              1.5e+004      1,5e+004      1,5e+004      1,5e+004  
//    E2             1.46E+004     1,46E+004     1,46E+004     1,46E+004  
//    F1               14603.0       14603,0       14603,0       14603,0  
//    G                  14603         14603         14603         14603  
//    N1              14,603.0      14 603,0      14.603,0      14.603,0  
//    P0           1,460,300 %   1 460 300 %    1.460.300%   1.460.300 %  
//    X4                  390B          390B          390B          390B  
//    000000.0000  014603.0000   014603,0000   014603,0000   014603,0000  
//    ##000.0          14603.0       14603,0       14603,0       14603,0  
Dim value As Int16 = 14603
Dim formats() As String = {"C", "D6", "e1", "E2", "F1", "G", "N1", _
                           "P0", "X4", "000000.0000", "##000.0"}
Dim providers() As CultureInfo = {New CultureInfo("en-us"), _
                                  New CultureInfo("fr-fr"), _
                                  New CultureInfo("de-de"), _
                                  New CultureInfo("es-es")}
' Display header.
Console.WriteLine("{0,24}{1,14}{2,14}{3,14}", providers(0), providers(1), _
                  providers(2), providers(3))
Console.WriteLine()                        
' Display a value using each format string.
For Each format As String In formats
   ' Display the value for each provider on the same line.
   Console.Write("{0,-12}", format)
   For Each provider As CultureInfo In providers
      Console.Write("{0,12}  ", _
                    value.ToString(format, provider)) 
   Next
   Console.WriteLine()
Next
' The example displays the following output to the console:
'                       en-US         fr-FR         de-DE         es-ES
'    
'    C             $14,603.00   14 603,00 €   14.603,00 €   14.603,00 €  
'    D6                014603        014603        014603        014603  
'    e1              1.5e+004      1,5e+004      1,5e+004      1,5e+004  
'    E2             1.46E+004     1,46E+004     1,46E+004     1,46E+004  
'    F1               14603.0       14603,0       14603,0       14603,0  
'    G                  14603         14603         14603         14603  
'    N1              14,603.0      14 603,0      14.603,0      14.603,0  
'    P0           1,460,300 %   1 460 300 %    1.460.300%   1.460.300 %  
'    X4                  390B          390B          390B          390B  
'    000000.0000  014603.0000   014603,0000   014603,0000   014603,0000  
'    ##000.0          14603.0       14603,0       14603,0       14603,0  

注解

ToString(String, IFormatProvider) 方法通过使用指定区域性的 NumberFormatInfo 对象来设置指定格式的 Int16 值的格式。The ToString(String, IFormatProvider) method formats an Int16 value in a specified format by using the NumberFormatInfo object 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 参数可以是标准或自定义数字格式字符串。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. 如果 format null 或空字符串(""),则此方法返回的字符串使用一般数值格式说明符("G")进行格式设置。If format is null or an empty string (""), the string returned by this method 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 实现。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 that is returned by this method. 实现 IFormatProvider 的对象可以是以下任一项:The object that implements IFormatProvider can be any of the following:

  • 一个表示要使用其格式设置规则的区域性的 CultureInfo 对象。A CultureInfo object that represents the culture whose formatting rules are to be used.

  • 一个包含此值的特定数字格式信息的 NumberFormatInfo 对象。A NumberFormatInfo object that contains specific numeric formatting information for this value.

  • 一个实现 IFormatProvider的自定义对象。A custom object that implements IFormatProvider.

如果 provider null,或者无法从 provider获取 NumberFormatInfo 对象,则返回值的格式为当前区域性的 NumberFormatInfoIf provider is null, or a NumberFormatInfo object cannot be obtained from provider, the return value is formatted with the NumberFormatInfo for the current culture.

另请参阅

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 numeric format string.

返回

此实例的值的字符串表示形式,由 format 指定。The string representation of the value of this instance as specified by format.

示例

下面的示例使用每个受支持的标准格式字符串和多个自定义格式字符串初始化两个 Int16 值并将它们显示到控制台。The following example initializes two Int16 values and displays them to the console using each of the supported standard format strings and several custom format strings. 此示例是以 en-us 作为当前区域性运行的。The example is run with en-US as the current culture.

Int16[] values = {-23805, 32194};
string[] formats = {"C4", "D6", "e1", "E2", "F1", "G", "N1", 
                    "P0", "X4", "000000.0000", "##000.0"};
foreach (string format in formats)
{
   Console.WriteLine("'{0,2}' format specifier: {1,17}   {2,17}",  
                     format, 
                     values[0].ToString(format), 
                     values[1].ToString(format));
}                                                               
// The example displays the following output to the console:
//    'C4' format specifier:    ($23,805.0000)        $32,194.0000
//    'D6' format specifier:           -023805              032194
//    'e1' format specifier:         -2.4e+004            3.2e+004
//    'E2' format specifier:        -2.38E+004           3.22E+004
//    'F1' format specifier:          -23805.0             32194.0
//    ' G' format specifier:            -23805               32194
//    'N1' format specifier:         -23,805.0            32,194.0
//    'P0' format specifier:      -2,380,500 %         3,219,400 %
//    'X4' format specifier:              A303                7DC2
//    '000000.0000' format specifier:      -023805.0000         032194.0000
//    '##000.0' format specifier:          -23805.0             32194.0      
Dim values() As Int16 = {-23805, 32194}
Dim formats() As String = {"C4", "D6", "e1", "E2", "F1", "G", "N1", _
                           "P0", "X4", "000000.0000", "##000.0"}
For Each format As String In formats
   Console.WriteLine("'{0,2}' format specifier: {1,17}   {2,17}", _ 
                     format, _
                     values(0).ToString(format), _
                     values(1).ToString(format))
Next                                                               
' The example displays the following output to the console:
'    'C4' format specifier:    ($23,805.0000)        $32,194.0000
'    'D6' format specifier:           -023805              032194
'    'e1' format specifier:         -2.4e+004            3.2e+004
'    'E2' format specifier:        -2.38E+004           3.22E+004
'    'F1' format specifier:          -23805.0             32194.0
'    ' G' format specifier:            -23805               32194
'    'N1' format specifier:         -23,805.0            32,194.0
'    'P0' format specifier:      -2,380,500 %         3,219,400 %
'    'X4' format specifier:              A303                7DC2
'    '000000.0000' format specifier:      -023805.0000         032194.0000
'    '##000.0' format specifier:          -23805.0             32194.0      

注解

ToString(String) 方法通过使用表示当前区域性约定的 NumberFormatInfo 对象来设置指定格式的 Int16 值的格式。The ToString(String) method formats an Int16 value in a specified format by using a NumberFormatInfo object that represents 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)

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. 如果 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:

此实例的返回值的格式为当前区域性的 NumberFormatInfoThe 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

返回

此实例的值的字符串表示形式,由减号(如果值为负)和范围从 0 到 9 且没有前导零的数字序列组成。The string representation of the value of this instance, consisting of a minus sign if the value is negative, and a sequence of digits ranging from 0 to 9 with no leading zeroes.

示例

下面的示例使用 ToString() 方法将 Int16 值的数组显示到控制台。The following example uses the ToString() method to display an array of Int16 values to the console.

short[] numbers = {0, 14624, 13982, short.MaxValue, 
                   short.MinValue, -16667};
foreach (short number in numbers)
{
   Console.WriteLine(number.ToString());
}            
// The example displays the following output to the console:
//       0
//       14624
//       13982
//       32767
//       -32768
//       -16667                             
Dim numbers() As Short = {0, 14624, 13982, Short.MaxValue, _
                         Short.MinValue, -16667}
For Each number As Short In numbers
   Console.WriteLine(number.ToString())
Next        
' The example displays the following output to the console:
'       0
'       14624
'       13982
'       32767
'       -32768
'       -16667                             

注解

ToString() 方法使用当前区域性的 NumberFormatInfo 对象,设置默认值("G" 或 "常规")格式的 Int16 值的格式。The ToString() method formats an Int16 value in the default ("G", or general) format by using the NumberFormatInfo object 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:

另请参阅

适用于