語言

UInt16.ToString 方法

定義

將這個實例的數值轉換為其相等的字串表示。

多載

名稱 Description
ToString()

將這個實例的數值轉換為其相等的字串表示。

ToString(IFormatProvider)

利用指定的文化特定格式資訊,將此實例的數值轉換為其等效字串表示。

ToString(String)

將此實例的數值轉換為其等效字串表示,並使用指定格式。

ToString(String, IFormatProvider)

使用指定的格式和文化特性特定格式資訊,將這個實例的數值轉換為其相等的字串表示。

ToString()

來源:
UInt16.cs
來源:
UInt16.cs
來源:
UInt16.cs
來源:
UInt16.cs
來源:
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)

來源:
UInt16.cs
來源:
UInt16.cs
來源:
UInt16.cs
來源:
UInt16.cs
來源:
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)

來源:
UInt16.cs
來源:
UInt16.cs
來源:
UInt16.cs
來源:
UInt16.cs
來源:
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) overload。

另請參閱

適用於

ToString(String, IFormatProvider)

來源:
UInt16.cs
來源:
UInt16.cs
來源:
UInt16.cs
來源:
UInt16.cs
來源:
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 當前文化的物件。

另請參閱

適用於