语言

UInt16.ToString 方法

定义

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

重载

名称 说明
ToString()

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

ToString(IFormatProvider)

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

ToString(String)

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

ToString(String, IFormatProvider)

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

ToString()

Source:
UInt16.cs
Source:
UInt16.cs
Source:
UInt16.cs
Source:
UInt16.cs
Source:
UInt16.cs

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

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

返回

此实例值的字符串表示形式,它由介于 0 到 9 的数字序列组成,不带符号或前导零。

示例

以下示例使用默认UInt16方法显示一个ToString()值。 它还显示使用某些标准格式说明符产生的值的字符串表示形式 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)

另请参阅

适用于

ToString(IFormatProvider)

Source:
UInt16.cs
Source:
UInt16.cs
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 -> string
Public 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
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 -> string
Public 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,则当前 UInt16 对象的返回值使用常规格式说明符(“G”)进行格式化。 如果 format 为任何其他值,该方法将引发一个 FormatException。

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

返回的字符串的格式由 NumberFormatInfo 当前区域性的对象确定。 根据 format 参数,此对象控制输出字符串中的符号,如组分隔符和小数点符号。 若要为当前区域性以外的区域性提供格式设置信息,请调用 ToString(String, IFormatProvider) 重载。

另请参阅

适用于

ToString(String, IFormatProvider)

Source:
UInt16.cs
Source:
UInt16.cs
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 -> string
Public 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,则当前 UInt16 对象的返回值使用常规格式说明符(“G”)进行格式化。 如果 format 为任何其他值,该方法将引发一个 FormatException。

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

参数 provider 是实现 IFormatProvider 。 其 GetFormat 方法返回一个 NumberFormatInfo 对象,该对象提供有关此方法返回的字符串格式的区域性特定信息。 ToString(String, IFormatProvider)调用该方法时,它将调用provider参数IFormatProvider.GetFormat的方法,并传递一个Type表示该NumberFormatInfo类型的对象。 然后,该方法 GetFormat 返回 NumberFormatInfo 提供设置当前 UInt16 值格式的信息的对象,例如组分隔符或小数点符号。 可通过三种方法使用 provider 参数向该方法提供格式设置信息 ToString(String, IFormatProvider) :

provider如果是null,则返回的字符串的格式基于NumberFormatInfo当前区域性的对象。

另请参阅

适用于