UInt16.ToString 方法 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将此实例的数值转换为其等效的字符串表示形式。
重载
| ToString(IFormatProvider) | 使用指定的区域性特定格式信息,将此实例的数值转换为它的等效字符串表示形式。 | 
| ToString(String) | 使用指定的格式将此实例的数值转换为它的等效字符串表示形式。 | 
| ToString(String, IFormatProvider) | 使用指定的格式和区域性特定格式信息,将此实例的数值转换为它的等效字符串表示形式。 | 
| ToString() | 将此实例的数值转换为其等效的字符串表示形式。 | 
ToString(IFormatProvider)
- Source:
- UInt16.cs
- Source:
- UInt16.cs
- Source:
- UInt16.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 -> stringPublic Function ToString (provider As IFormatProvider) As String参数
- provider
- IFormatProvider
一个提供区域性特定的格式设置信息的对象。
返回
此实例的值的字符串表示形式,由范围从 0 到 9 的数字序列组成,不包含符号或前导零。
实现
示例
以下示例使用多个格式提供程序(包括一个用于固定区域性的提供程序)设置 16 位带符号整数值的格式。 该示例的输出表明,无论格式提供程序如何,方法返回 ToString(IFormatProvider) 的格式化字符串都是相同的。
using System;
using System.Globalization;
public class Example
{
   public static void Main()
   {
      // Define an array of CultureInfo objects.
      CultureInfo[] ci = { new CultureInfo("en-US"), 
                           new CultureInfo("fr-FR"), 
                           CultureInfo.InvariantCulture }; 
      UInt16 value = 18924;
      Console.WriteLine("  {0,12}   {1,12}   {2,12}", 
                        GetName(ci[0]), GetName(ci[1]), GetName(ci[2])); 
      Console.WriteLine("  {0,12}   {1,12}   {2,12}", 
                        value.ToString(ci[0]), value.ToString(ci[1]), value.ToString(ci[2]));   
   }
   private static string GetName(CultureInfo ci)
   {
      if (ci.Equals(CultureInfo.InvariantCulture))
         return "Invariant";
      else
         return ci.Name;         
   }
}
// The example displays the following output:
//          en-US          fr-FR      Invariant
//          18924          18924          18924
open System.Globalization
let getName (ci: CultureInfo) =
    if ci.Equals CultureInfo.InvariantCulture then "Invariant"
    else ci.Name
// Define an array of CultureInfo objects.
let ci = 
    [| CultureInfo "en-US" 
       CultureInfo "fr-FR" 
       CultureInfo.InvariantCulture |] 
let value = 18924us
printfn $"  {getName ci[0],12}   {getName ci[1],12}   {getName ci[2],12}"
printfn $"  {value.ToString ci[0],12}   {value.ToString ci[1],12}   {value.ToString ci[2],12}"
// The example displays the following output:
//          en-US          fr-FR      Invariant
//          18924          18924          18924
Imports System.Globalization
Module Example
   Public Sub Main()
      ' Define an array of CultureInfo objects.
      Dim ci() As CultureInfo = { New CultureInfo("en-US"), _
                                  New CultureInfo("fr-FR"), _
                                  CultureInfo.InvariantCulture } 
      Dim value As UInt16 = 18924
      Console.WriteLine("  {0,12}   {1,12}   {2,12}", _
                        GetName(ci(0)), GetName(ci(1)), GetName(ci(2))) 
      Console.WriteLine("  {0,12}   {1,12}   {2,12}", _
                        value.ToString(ci(0)), value.ToString(ci(1)), value.ToString(ci(2)))            
      
   End Sub
   
   Private Function GetName(ci As CultureInfo) As String
      If ci.Equals(CultureInfo.InvariantCulture) Then
         Return "Invariant"
      Else
         Return ci.Name
      End If   
   End Function
End Module
' The example displays the following output:
'         en-US          fr-FR      Invariant
'         18924          18924          18924
注解
方法ToString(IFormatProvider)UInt16使用NumberFormatInfo指定区域性的 对象设置默认 (“G”或常规) 格式的值。 如果要指定其他格式或当前区域性,请使用 方法的其他重载 ToString ,如下所示:
| 使用格式 | 对于区域性 | 使用重载 | 
|---|---|---|
| 默认 (“G”) 格式 | 默认 (当前) 区域性 | ToString() | 
| 特定格式 | 默认 (当前) 区域性 | ToString(String) | 
| 特定格式 | 特定区域性 | ToString(String, IFormatProvider) | 
参数 provider 是实现 IFormatProvider 。 其 GetFormat 方法返回一个 NumberFormatInfo 对象,该对象提供特定于区域性的格式设置信息。 但是,当使用常规数字格式说明符 (“G”) 进行格式设置时,不使用 的任何属性 NumberFormatInfo 。
另请参阅
适用于
ToString(String)
- Source:
- UInt16.cs
- Source:
- UInt16.cs
- Source:
- UInt16.cs
使用指定的格式将此实例的数值转换为它的等效字符串表示形式。
public:
 System::String ^ ToString(System::String ^ format);public string ToString (string format);public string ToString (string? format);override this.ToString : string -> stringPublic Function ToString (format As String) As String参数
- format
- String
一个数值格式字符串。
返回
此实例的值的字符串表示形式,由 format 指定。
例外
              format 参数无效。
示例
以下示例通过使用每个标准格式字符串和一些自定义格式字符串来显示一个 16 位无符号整数值。
using System;
using System.Globalization;
public class Example
{
   public static void Main()
   {
      ushort value = 21708;
      string[] specifiers = { "G", "C", "D3", "E2", "e3", "F", 
                              "N", "P", "X", "000000.0", "#.0", 
                              "00000000;(0);**Zero**" };
      
      foreach (string specifier in specifiers)
         Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
   }
}
// The example displays the following output:
//       G: 21708
//       C: $21,708.00
//       D3: 21708
//       E2: 2.17E+004
//       e3: 2.171e+004
//       F: 21708.00
//       N: 21,708.00
//       P: 2,170,800.00 %
//       X: 54CC
//       000000.0: 021708.0
//       #.0: 21708.0
//       00000000;(0);**Zero**: 00021708
let value = 21708us
let specifiers = 
    [| "G"; "C"; "D3"; "E2"; "e3"; "F"
       "N"; "P"; "X"; "000000.0"; "#.0" 
       "00000000(0)**Zero**" |]
for specifier in specifiers do
    printfn $"{specifier}: {value.ToString specifier}"
// The example displays the following output:
//       G: 21708
//       C: $21,708.00
//       D3: 21708
//       E2: 2.17E+004
//       e3: 2.171e+004
//       F: 21708.00
//       N: 21,708.00
//       P: 2,170,800.00 %
//       X: 54CC
//       000000.0: 021708.0
//       #.0: 21708.0
//       00000000(0)**Zero**: 00021708
Imports System.Globalization
Module Example
   Public Sub Main()
      Dim value As UShort = 21708 
      Dim specifiers() As String = { "G", "C", "D3", "E2", "e3", "F", _
                                     "N", "P", "X", "000000.0", "#.0", _
                                     "00000000;(0);**Zero**" }
      
      For Each specifier As String In specifiers
         Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
      Next
   End Sub
End Module
' The example displays the following output:
'       G: 21708
'       C: $21,708.00
'       D3: 21708
'       E2: 2.17E+004
'       e3: 2.171e+004
'       F: 21708.00
'       N: 21,708.00
'       P: 2,170,800.00 %
'       X: 54CC
'       000000.0: 021708.0
'       #.0: 21708.0
'       00000000;(0);**Zero**: 00021708
注解
方法ToString(String)UInt16使用NumberFormatInfo表示当前区域性约定的 对象以指定格式设置值的格式。 如果要使用默认 (“G”或常规) 格式或指定其他区域性,请使用 方法的其他重载 ToString ,如下所示:
| 使用格式 | 对于区域性 | 使用重载 | 
|---|---|---|
| 默认 (“G”) 格式 | 默认 (当前) 区域性 | ToString() | 
| 默认 (“G”) 格式 | 特定区域性 | ToString(IFormatProvider) | 
| 特定格式 | 特定区域性 | ToString(String, IFormatProvider) | 
参数 format 可以是任何有效的 标准数字格式说明符,也可以是 自定义数字格式说明符的任意组合。 如果 format 等于 String.Empty 或 为 null,则使用常规格式说明符 (“G”) 设置当前 UInt16 对象的返回值的格式。 如果 format 是任何其他值,该方法将 FormatException引发 。
.NET 提供广泛的格式设置支持,以下格式设置主题对此进行了更详细的介绍:
- 有关数字格式说明符的详细信息,请参阅 标准数字格式字符串 和 自定义数值格式字符串。 
- 有关 .NET 中对格式设置的支持的详细信息,请参阅 格式设置类型。 
返回的字符串的格式由 NumberFormatInfo 当前区域性的 对象确定。 根据 format 参数,此对象控制输出字符串中的组分隔符和小数点符号等符号。 若要为当前区域性以外的区域性提供格式设置信息,请 ToString(String, IFormatProvider) 调用 重载。
另请参阅
适用于
ToString(String, IFormatProvider)
- Source:
- UInt16.cs
- Source:
- UInt16.cs
- Source:
- UInt16.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 -> stringPublic Function ToString (format As String, provider As IFormatProvider) As String参数
- format
- String
一个数值格式字符串。
- provider
- IFormatProvider
一个提供区域性特定的格式设置信息的对象。
返回
此实例的值的字符串表示形式,由 format 和 provider 指定。
实现
例外
              format 无效。
示例
下面的示例通过使用标准数值格式说明符和一些特定 CultureInfo 对象来显示一个 16 位无符号整数值。
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") };
      string[] specifiers = {"G", "C", "D4", "E2", "F", "N", "P", "X2"}; 
      ushort value = 22042;
      
      foreach (string specifier in specifiers)
      {
         foreach (CultureInfo culture in cultures)
            Console.WriteLine("{0,2} format using {1} culture: {2, 16}",  
                              specifier, culture.Name, 
                              value.ToString(specifier, culture));
         Console.WriteLine();
      }
   }
}
// The example displays the following output:
//        G format using en-US culture:            22042
//        G format using fr-FR culture:            22042
//        G format using es-ES culture:            22042
//       
//        C format using en-US culture:       $22,042.00
//        C format using fr-FR culture:      22 042,00 €
//        C format using es-ES culture:      22.042,00 €
//       
//       D4 format using en-US culture:            22042
//       D4 format using fr-FR culture:            22042
//       D4 format using es-ES culture:            22042
//       
//       E2 format using en-US culture:        2.20E+004
//       E2 format using fr-FR culture:        2,20E+004
//       E2 format using es-ES culture:        2,20E+004
//       
//        F format using en-US culture:         22042.00
//        F format using fr-FR culture:         22042,00
//        F format using es-ES culture:         22042,00
//       
//        N format using en-US culture:        22,042.00
//        N format using fr-FR culture:        22 042,00
//        N format using es-ES culture:        22.042,00
//       
//        P format using en-US culture:   2,204,200.00 %
//        P format using fr-FR culture:   2 204 200,00 %
//        P format using es-ES culture:   2.204.200,00 %
//       
//       X2 format using en-US culture:             561A
//       X2 format using fr-FR culture:             561A
//       X2 format using es-ES culture:             561A
open System.Globalization
// Define cultures whose formatting conventions are to be used.
let cultures = 
    [| CultureInfo.CreateSpecificCulture "en-US" 
       CultureInfo.CreateSpecificCulture "fr-FR"
       CultureInfo.CreateSpecificCulture "es-ES" |]
let specifiers = [| "G"; "C"; "D4"; "E2"; "F"; "N"; "P"; "X2" |] 
let value = 22042us
for specifier in specifiers do
    for culture in cultures do
        printfn $"{specifier,2} format using {culture.Name} culture: {value.ToString(specifier, culture), 16}"
    printfn ""
// The example displays the following output:
//        G format using en-US culture:            22042
//        G format using fr-FR culture:            22042
//        G format using es-ES culture:            22042
//       
//        C format using en-US culture:       $22,042.00
//        C format using fr-FR culture:      22 042,00 €
//        C format using es-ES culture:      22.042,00 €
//       
//       D4 format using en-US culture:            22042
//       D4 format using fr-FR culture:            22042
//       D4 format using es-ES culture:            22042
//       
//       E2 format using en-US culture:        2.20E+004
//       E2 format using fr-FR culture:        2,20E+004
//       E2 format using es-ES culture:        2,20E+004
//       
//        F format using en-US culture:         22042.00
//        F format using fr-FR culture:         22042,00
//        F format using es-ES culture:         22042,00
//       
//        N format using en-US culture:        22,042.00
//        N format using fr-FR culture:        22 042,00
//        N format using es-ES culture:        22.042,00
//       
//        P format using en-US culture:   2,204,200.00 %
//        P format using fr-FR culture:   2 204 200,00 %
//        P format using es-ES culture:   2.204.200,00 %
//       
//       X2 format using en-US culture:             561A
//       X2 format using fr-FR culture:             561A
//       X2 format using es-ES culture:             561A
Imports 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 specifiers() As String = {"G", "C", "D4", "E2", "F", "N", "P", "X2"} 
      Dim value As UShort = 22042
      
      For Each specifier As String In specifiers
         For Each culture As CultureInfo In Cultures
            Console.WriteLine("{0,2} format using {1} culture: {2, 16}", _ 
                              specifier, culture.Name, _
                              value.ToString(specifier, culture))
         Next
         Console.WriteLine()
      Next
   End Sub
End Module
' The example displays the following output:
'        G format using en-US culture:            22042
'        G format using fr-FR culture:            22042
'        G format using es-ES culture:            22042
'       
'        C format using en-US culture:       $22,042.00
'        C format using fr-FR culture:      22 042,00 €
'        C format using es-ES culture:      22.042,00 €
'       
'       D4 format using en-US culture:            22042
'       D4 format using fr-FR culture:            22042
'       D4 format using es-ES culture:            22042
'       
'       E2 format using en-US culture:        2.20E+004
'       E2 format using fr-FR culture:        2,20E+004
'       E2 format using es-ES culture:        2,20E+004
'       
'        F format using en-US culture:         22042.00
'        F format using fr-FR culture:         22042,00
'        F format using es-ES culture:         22042,00
'       
'        N format using en-US culture:        22,042.00
'        N format using fr-FR culture:        22 042,00
'        N format using es-ES culture:        22.042,00
'       
'        P format using en-US culture:   2,204,200.00 %
'        P format using fr-FR culture:   2 204 200,00 %
'        P format using es-ES culture:   2.204.200,00 %
'       
'       X2 format using en-US culture:             561A
'       X2 format using fr-FR culture:             561A
'       X2 format using es-ES culture:             561A
注解
方法ToString(String, IFormatProvider)UInt16使用NumberFormatInfo指定区域性的 对象,以指定格式设置值的格式。 如果要使用默认格式或区域性设置,请使用 方法的其他重载 ToString ,如下所示:
| 使用格式 | 对于区域性 | 使用重载 | 
|---|---|---|
| 默认 (“G”) 格式 | 默认 (当前) 区域性 | ToString() | 
| 默认 (“G”) 格式 | 特定区域性 | ToString(IFormatProvider) | 
| 特定格式 | 默认 (当前) 区域性 | ToString(String) | 
参数 format 可以是任何有效的 标准数字格式字符串,也可以是 自定义数字格式字符串的任意组合。 如果 format 等于 String.Empty 或 为 null,则使用常规格式说明符 (“G”) 设置当前 UInt16 对象的返回值的格式。 如果 format 是任何其他值,该方法将 FormatException引发 。
.NET 提供广泛的格式设置支持,以下格式设置主题对此进行了更详细的介绍:
- 有关数字格式说明符的详细信息,请参阅 标准数字格式字符串 和 自定义数值格式字符串。 
- 有关格式设置的详细信息,请参阅 格式设置类型。 
参数 provider 是实现 IFormatProvider 。 其 GetFormat 方法返回一个 NumberFormatInfo 对象,该对象提供有关此方法返回的字符串格式的区域性特定信息。 
              ToString(String, IFormatProvider)调用 方法时,它会调用provider参数的 IFormatProvider.GetFormat 方法,并向其传递表示Type类型的 NumberFormatInfo 对象。 然后, GetFormat 方法返回 NumberFormatInfo 对象,该对象提供设置当前 UInt16 值格式的信息,例如组分隔符符号或小数点符号。 有三种方法可以使用 provider 参数向 方法提供格式设置信息 ToString(String, IFormatProvider) :
- 可以传递一个 CultureInfo 对象,该对象表示提供格式设置信息的区域性。 其 GetFormat 方法返回对象, NumberFormatInfo 该对象提供该区域性的数字格式设置信息。 
- 可以传递提供数字格式设置信息的实际 NumberFormatInfo 对象。 (它的 实现 GetFormat 只返回自身。) 
- 可以传递实现 的 IFormatProvider自定义对象。 其 GetFormat 方法实例化并返回 NumberFormatInfo 提供格式设置信息的 对象。 
如果 provider 为 null,则返回的字符串的格式基于 NumberFormatInfo 当前区域性的 对象。
另请参阅
- Parse(String)
- 设置 .NET 中类型的格式
- 如何:用前导零填充数字
- 示例:.NET Core WinForms 格式设置实用工具 (C#)
- 示例:.NET Core WinForms 格式设置实用工具 (Visual Basic)
适用于
ToString()
- Source:
- UInt16.cs
- Source:
- UInt16.cs
- Source:
- UInt16.cs
将此实例的数值转换为其等效的字符串表示形式。
public:
 override System::String ^ ToString();public override string ToString ();override this.ToString : unit -> stringPublic Overrides Function ToString () As String返回
此实例的值的字符串表示形式,由范围从 0 到 9 的数字序列组成,不包含符号或前导零。
示例
以下示例使用默认ToString()方法显示一个UInt16值。 它还显示使用某些标准格式说明符产生的值的字符串表示形式 UInt16 。 使用 en-US 区域性的格式设置约定显示示例。
using System;
public class Example
{
   public static void Main()
   {
      ushort value = 16324;
      // Display value using default ToString method.
      Console.WriteLine(value.ToString());      
      Console.WriteLine();
      
      // Define an array of format specifiers.
      string[] formats = { "G", "C", "D", "F", "N", "X" };
      // Display value using the standard format specifiers.
      foreach (string format in formats)
         Console.WriteLine("{0} format specifier: {1,12}", 
                           format, value.ToString(format));         
   }
}
// The example displays the following output:
//       16324
//
//       G format specifier:        16324
//       C format specifier:   $16,324.00
//       D format specifier:        16324
//       F format specifier:     16324.00
//       N format specifier:    16,324.00
//       X format specifier:         3FC4
let value = 16324us
// Display value using default ToString method.
printfn $"{value.ToString()}\n"     
// Define an array of format specifiers.
let formats = [| "G"; "C"; "D"; "F"; "N"; "X" |]
// Display value using the standard format specifiers.
for format in formats do
    printfn $"{format} format specifier: {value.ToString format,12}" 
// The example displays the following output:
//       16324
//
//       G format specifier:        16324
//       C format specifier:   $16,324.00
//       D format specifier:        16324
//       F format specifier:     16324.00
//       N format specifier:    16,324.00
//       X format specifier:         3FC4
Module Example
   Public Sub Main()
      Dim value As UInt16 = 16324
      ' Display value using default ToString method.
      Console.WriteLine(value.ToString())            
      Console.WriteLine()
      
      ' Define an array of format specifiers.
      Dim formats() As String = { "G", "C", "D", "F", "N", "X" }
      ' Display value using the standard format specifiers.
      For Each format As String In formats
         Console.WriteLine("{0} format specifier: {1,12}", _
                           format, value.ToString(format))         
      Next
   End Sub
End Module
' The example displays the following output:
'       16324
'       
'       G format specifier:        16324
'       C format specifier:   $16,324.00
'       D format specifier:        16324
'       F format specifier:     16324.00
'       N format specifier:    16,324.00
'       X format specifier:         3FC4
注解
方法ToString()UInt16使用NumberFormatInfo当前区域性的 对象设置默认 (“G”或常规) 格式的值。 如果要指定不同的格式或区域性,请使用 方法的其他重载 ToString ,如下所示:
| 使用格式 | 对于区域性 | 使用重载 | 
|---|---|---|
| 默认 (“G”) 格式 | 特定区域性 | ToString(IFormatProvider) | 
| 特定格式 | 默认 (当前) 区域性 | ToString(String) | 
| 特定格式 | 特定区域性 | ToString(String, IFormatProvider) |