Int16.ToString 方法

定义

将此实例的数值转换为其等效的字符串表示形式。

重载

名称 说明
ToString(IFormatProvider)

使用指定的区域性特定格式信息将此实例的数值转换为其等效的字符串表示形式。

ToString()

将此实例的数值转换为其等效的字符串表示形式。

ToString(String)

使用指定的格式将此实例的数值转换为其等效的字符串表示形式。

ToString(String, IFormatProvider)

使用指定的格式和区域性特定的格式设置信息,将此实例的数值转换为其等效的字符串表示形式。

ToString(IFormatProvider)

Source:
Int16.cs
Source:
Int16.cs
Source:
Int16.cs
Source:
Int16.cs
Source:
Int16.cs

使用指定的区域性特定格式信息将此实例的数值转换为其等效的字符串表示形式。

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

提供 IFormatProvider 区域性特定的格式设置信息。

返回

此实例的值的字符串表示形式,由 provider.

实现

示例

下面的示例循环访问一个值数组 Int16 ,并通过调用 Int16.ToString(IFormatProvider) 具有不同格式提供程序的方法向控制台显示每个值。 由于默认的“G”格式说明符定义的简单格式设置,因此无论参数的值Int16如何,为每个provider值生成的格式化字符串都是相同的。

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)
let numbers = [ -23092s; 0s; 14894s; Int16.MaxValue ]
let providers = 
    [ CultureInfo "en-us"
      CultureInfo "fr-fr"
      CultureInfo "de-de"
      CultureInfo "es-es" ]

for int16Value in numbers do
    for provider in providers do
        printf $"{int16Value.ToString provider, 6} ({provider.Name})     "
                    
    printfn ""

// 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)Int16使用NumberFormatInfo指定区域性的对象设置默认格式(“G”或常规)格式的值的格式。 如果要指定其他格式或当前区域性,请使用该方法的其他重载 ToString ,如下所示:

使用格式 对于区域性 使用重载
默认格式(“G”) 默认(当前)区域性 ToString()
特定格式 默认(当前)区域性 ToString(String)
特定格式 特定区域性 ToString(String, IFormatProvider)

.NET 提供广泛的格式支持,在以下格式设置主题中对此进行了更详细的描述:

provider 参数是一个 IFormatProvider 实现,其 IFormatProvider.GetFormat 方法返回对象 NumberFormatInfo 。 通常, provider 是对象 NumberFormatInfoCultureInfo 对象。 该 NumberFormatInfo 对象提供有关此方法返回的字符串格式的区域性特定信息。 provider如果是null,则此实例的格式为NumberFormatInfo当前区域性的对象。

另请参阅

适用于

ToString()

Source:
Int16.cs
Source:
Int16.cs
Source:
Int16.cs
Source:
Int16.cs
Source:
Int16.cs

将此实例的数值转换为其等效的字符串表示形式。

public:
 override System::String ^ ToString();
public override string ToString();
override this.ToString : unit -> string
Public Overrides Function ToString () As String

返回

此实例值的字符串表示形式,如果值为负数,则由减号组成,数字序列从 0 到 9 不等,不带前导零。

示例

以下示例使用 ToString() 该方法向控制台显示值数组 Int16

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
let numbers = [ 0s; 14624s; 13982s; Int16.MaxValue; Int16.MinValue; -16667s ]

for number in numbers do
    printfn $"{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()Int16使用NumberFormatInfo当前区域性的对象设置默认格式(“G”或常规)格式的值的格式。 如果要指定其他格式或区域性,请使用该方法的其他重载 ToString ,如下所示:

使用格式 对于区域性 使用重载
默认格式(“G”) 特定区域性 ToString(IFormatProvider)
特定格式 默认(当前)区域性 ToString(String)
特定格式 特定区域性 ToString(String, IFormatProvider)

.NET 提供广泛的格式支持,在以下格式设置主题中对此进行了更详细的描述:

另请参阅

适用于

ToString(String)

Source:
Int16.cs
Source:
Int16.cs
Source:
Int16.cs
Source:
Int16.cs
Source:
Int16.cs

使用指定的格式将此实例的数值转换为其等效的字符串表示形式。

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

数字格式字符串。

返回

此实例的值的字符串表示形式,由 format.

示例

以下示例初始化两个 Int16 值,并使用每个受支持的标准格式字符串和多个自定义格式字符串将其显示给控制台。 此示例以 en-US 作为当前区域性运行。

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
let values = [| -23805s; 32194s |]
let formats = 
    [ "C4"; "D6"; "e1"; "E2"; "F1"; "G"; "N1"
      "P0"; "X4"; "000000.0000"; "##000.0" ]
for format in formats do
    printfn $"'{format,2}' format specifier: {values[0].ToString format,17}   {values[1].ToString format,17}"

// 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)Int16使用NumberFormatInfo表示当前区域性约定的对象设置指定格式的值的格式。 如果要使用默认格式(“G”或常规)格式或指定其他区域性,请使用该方法的其他重载 ToString ,如下所示:

使用格式 对于区域性 使用重载
默认格式(“G”) 默认(当前)区域性 ToString()
默认格式(“G”) 特定区域性 ToString(IFormatProvider)
特定格式 特定区域性 ToString(String, IFormatProvider)

format 参数可以是标准或自定义数字格式字符串。 支持除“R”(或“r”)以外的所有标准数值格式字符串,所有自定义数字格式字符都支持。 如果 formatnull 空字符串或空字符串,则此实例的返回值使用常规数值格式说明符(“G”)进行格式化。

.NET 提供广泛的格式支持,在以下格式设置主题中对此进行了更详细的描述:

此实例的返回值采用当前区域性的格式 NumberFormatInfo

另请参阅

适用于

ToString(String, IFormatProvider)

Source:
Int16.cs
Source:
Int16.cs
Source:
Int16.cs
Source:
Int16.cs
Source:
Int16.cs

使用指定的格式和区域性特定的格式设置信息,将此实例的数值转换为其等效的字符串表示形式。

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

数字格式字符串。

provider
IFormatProvider

提供区域性特定的格式设置信息的对象。

返回

此实例的值的字符串表示形式,由 formatprovider指定 。

实现

示例

以下示例使用四种不同区域性中每个受支持的标准格式字符串显示一个 Int16 值。

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
 let value = 14603
 let formats =
     [ "C"; "D6"; "e1"; "E2"; "F1"; "G"; "N1"
       "P0"; "X4"; "000000.0000"; "##000.0" ]
 let providers = 
     [ CultureInfo "en-us"
       CultureInfo "fr-fr"
       CultureInfo "de-de"
       CultureInfo "es-es" ]
 // Display header.
 printfn $"{providers[0],24}{providers[1],14}{providers[2],14}{providers[3],14}\n"
 
 // Display a value using each format string.
 for format in formats do
     // Display the value for each provider on the same line.
     printf $"{format,-12}"
     for provider in providers do
         printf $"{value.ToString(format, provider),12}  "
     printfn ""

// 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)Int16使用NumberFormatInfo指定区域性的对象设置指定格式的值的格式。 如果要使用默认格式或区域性设置,请使用该方法的其他重载 ToString ,如下所示:

使用格式 对于区域性 使用重载
默认格式(“G”) 默认(当前)区域性 ToString()
默认格式(“G”) 特定区域性 ToString(IFormatProvider)
特定格式 默认(当前)区域性 ToString(String)

format 参数可以是标准或自定义数字格式字符串。 支持除“R”(或“r”)以外的所有标准数值格式字符串,所有自定义数字格式字符都支持。 如果 formatnull 空字符串(“”),则此方法返回的字符串使用常规数值格式说明符(“G”)进行格式化。

.NET 提供广泛的格式支持,在以下格式设置主题中对此进行了更详细的描述:

参数 provider 是实现 IFormatProvider 。 其 GetFormat 方法返回一个 NumberFormatInfo 对象,该对象提供有关此方法返回的字符串格式的区域性特定信息。 实现 IFormatProvider 的对象可以是以下任一对象:

如果providernull,或者无法从NumberFormatInfo中获取对象provider,则返回值采用当前区域性的格式NumberFormatInfo

另请参阅

适用于