語言

UInt32.ToString 方法

定義

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

多載

名稱 Description
ToString(IFormatProvider)

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

ToString(String)

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

ToString(String, IFormatProvider)

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

ToString()

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

ToString(IFormatProvider)

來源:
UInt32.cs
來源:
UInt32.cs
來源:
UInt32.cs
來源:
UInt32.cs
來源:
UInt32.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 }; 
      uint value = 1870924;
      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
//           1870924        1870924        1870924
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 = 1870924u

printfn $"  {getName ci[0],12}   {getName ci[1],12}   {getName ci[3],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
//           1870924        1870924        1870924
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 UInteger = 1870924
      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
'      1870924        1870924        1870924

備註

該ToString(IFormatProvider)方法透過使用UInt32指定文化的物件,將NumberFormatInfo值格式化為預設(「G」或一般格式)。 如果你想指定不同的格式或目前的文化,請使用方法的其他超載 ToString ,如下:

使用格式 為了文化 利用過載
預設(「G」)格式 預設(現行)文化 ToString()
特定格式 預設(現行)文化 ToString(String)
特定格式 特定文化 ToString(String, IFormatProvider)

參數 provider 是一個 IFormatProvider 實作。 其 GetFormat 方法回傳 NumberFormatInfo 一個提供特定文化格式資訊的物件。 然而,在格式化一般數字格式指定符(“G”)時,這些屬性 NumberFormatInfo 並未被使用。

另請參閱

適用於

ToString(String)

來源:
UInt32.cs
來源:
UInt32.cs
來源:
UInt32.cs
來源:
UInt32.cs
來源:
UInt32.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 參數無效。

範例

以下範例透過使用每個標準格式字串及部分自訂格式字串,顯示一個 32 位元無號整數值。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      uint value = 2179608;
      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: 2179608
//       C: $2,179,608.00
//       D3: 2179608
//       E2: 2.18E+006
//       e3: 2.180e+006
//       F: 2179608.00
//       N: 2,179,608.00
//       P: 217,960,800.00 %
//       X: 214218
//       000000.0: 2179608.0
//       #.0: 2179608.0
//       00000000;(0);**Zero**: 02179608
let value = 2179608u
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: 2179608
//       C: $2,179,608.00
//       D3: 2179608
//       E2: 2.18E+006
//       e3: 2.180e+006
//       F: 2179608.00
//       N: 2,179,608.00
//       P: 217,960,800.00 %
//       X: 214218
//       000000.0: 2179608.0
//       #.0: 2179608.0
//       00000000(0)**Zero**: 02179608
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim value As UInteger = 2179608 
      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: 2179608
'       C: $2,179,608.00
'       D3: 2179608
'       E2: 2.18E+006
'       e3: 2.180e+006
'       F: 2179608.00
'       N: 2,179,608.00
'       P: 217,960,800.00 %
'       X: 214218
'       000000.0: 2179608.0
'       #.0: 2179608.0
'       00000000;(0);**Zero**: 02179608

備註

該ToString(String)方法透過使用UInt32代表當前文化慣例的物件,將NumberFormatInfo值格式化成指定格式。 如果你想使用預設的(「G」或一般)格式或指定不同的文化,請使用方法的其他 ToString 超載,如下:

使用格式 為了文化 利用過載
預設(「G」)格式 預設(現行)文化 ToString()
預設(「G」)格式 特定文化 ToString(IFormatProvider)
特定格式 特定文化 ToString(String, IFormatProvider)

format參數可以是任何有效的標準數值格式字串,或是自訂數值格式字串的任意組合。 若 format 等 String.Empty 於 或 , null則當前 UInt32 物件的回傳值會以一般格式指定符(“G”)格式化。 如果 format 為任何其他值,方法會擲回 FormatException。

.NET 提供廣泛的格式支援,如下列格式化主題中更詳細地說明:

回傳字串的格式由 NumberFormatInfo 當前文化的物件決定。 根據參數不同 format ,此物件控制像是輸出字串中的分隔符和小數點符號等符號。 若要提供非當前文化的格式資訊,請呼叫 ToString(String, IFormatProvider) overload。

另請參閱

適用於

ToString(String, IFormatProvider)

來源:
UInt32.cs
來源:
UInt32.cs
來源:
UInt32.cs
來源:
UInt32.cs
來源:
UInt32.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 物件,顯示一個 32 位元無號整數值。

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"}; 
      uint value = 2222402;
      
      foreach (string specifier in specifiers)
      {
         foreach (CultureInfo culture in cultures)
            Console.WriteLine("{0,2} format using {1} culture: {2, 18}",  
                              specifier, culture.Name, 
                              value.ToString(specifier, culture));
         Console.WriteLine();
      }
   }
}
// The example displays the following output:
//        G format using en-US culture:            2222402
//        G format using fr-FR culture:            2222402
//        G format using es-ES culture:            2222402
//       
//        C format using en-US culture:      $2,222,402.00
//        C format using fr-FR culture:     2 222 402,00 €
//        C format using es-ES culture:     2.222.402,00 €
//       
//       D4 format using en-US culture:            2222402
//       D4 format using fr-FR culture:            2222402
//       D4 format using es-ES culture:            2222402
//       
//       E2 format using en-US culture:          2.22E+006
//       E2 format using fr-FR culture:          2,22E+006
//       E2 format using es-ES culture:          2,22E+006
//       
//        F format using en-US culture:         2222402.00
//        F format using fr-FR culture:         2222402,00
//        F format using es-ES culture:         2222402,00
//       
//        N format using en-US culture:       2,222,402.00
//        N format using fr-FR culture:       2 222 402,00
//        N format using es-ES culture:       2.222.402,00
//       
//        P format using en-US culture:   222,240,200.00 %
//        P format using fr-FR culture:   222 240 200,00 %
//        P format using es-ES culture:   222.240.200,00 %
//       
//       X2 format using en-US culture:             21E942
//       X2 format using fr-FR culture:             21E942
//       X2 format using es-ES culture:             21E942
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 = 2222402

for specifier in specifiers do
    for culture in cultures do
        printfn $"{specifier,2} format using {culture.Name} culture: {value.ToString(specifier, culture), 18}"
    printfn ""
// The example displays the following output:
//        G format using en-US culture:            2222402
//        G format using fr-FR culture:            2222402
//        G format using es-ES culture:            2222402
//       
//        C format using en-US culture:      $2,222,402.00
//        C format using fr-FR culture:     2 222 402,00 €
//        C format using es-ES culture:     2.222.402,00 €
//       
//       D4 format using en-US culture:            2222402
//       D4 format using fr-FR culture:            2222402
//       D4 format using es-ES culture:            2222402
//       
//       E2 format using en-US culture:          2.22E+006
//       E2 format using fr-FR culture:          2,22E+006
//       E2 format using es-ES culture:          2,22E+006
//       
//        F format using en-US culture:         2222402.00
//        F format using fr-FR culture:         2222402,00
//        F format using es-ES culture:         2222402,00
//       
//        N format using en-US culture:       2,222,402.00
//        N format using fr-FR culture:       2 222 402,00
//        N format using es-ES culture:       2.222.402,00
//       
//        P format using en-US culture:   222,240,200.00 %
//        P format using fr-FR culture:   222 240 200,00 %
//        P format using es-ES culture:   222.240.200,00 %
//       
//       X2 format using en-US culture:             21E942
//       X2 format using fr-FR culture:             21E942
//       X2 format using es-ES culture:             21E942
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 UInteger = 2222402
      
      For Each specifier As String In specifiers
         For Each culture As CultureInfo In Cultures
            Console.WriteLine("{0,2} format using {1} culture: {2, 18}", _ 
                              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:            2222402
'        G format using fr-FR culture:            2222402
'        G format using es-ES culture:            2222402
'       
'        C format using en-US culture:      $2,222,402.00
'        C format using fr-FR culture:     2 222 402,00 €
'        C format using es-ES culture:     2.222.402,00 €
'       
'       D4 format using en-US culture:            2222402
'       D4 format using fr-FR culture:            2222402
'       D4 format using es-ES culture:            2222402
'       
'       E2 format using en-US culture:          2.22E+006
'       E2 format using fr-FR culture:          2,22E+006
'       E2 format using es-ES culture:          2,22E+006
'       
'        F format using en-US culture:         2222402.00
'        F format using fr-FR culture:         2222402,00
'        F format using es-ES culture:         2222402,00
'       
'        N format using en-US culture:       2,222,402.00
'        N format using fr-FR culture:       2 222 402,00
'        N format using es-ES culture:       2.222.402,00
'       
'        P format using en-US culture:   222,240,200.00 %
'        P format using fr-FR culture:   222 240 200,00 %
'        P format using es-ES culture:   222.240.200,00 %
'       
'       X2 format using en-US culture:             21E942
'       X2 format using fr-FR culture:             21E942
'       X2 format using es-ES culture:             21E942

備註

該ToString(String, IFormatProvider)方法透過使用UInt32指定文化的物件,將NumberFormatInfo值格式化成特定格式。 如果你想使用預設格式或文化設定,請使用方法的其他超載 ToString ,如下:

使用格式 為了文化 利用過載
預設(「G」)格式 預設(現行)文化 ToString()
預設(「G」)格式 特定文化 ToString(IFormatProvider)
特定格式 預設(現行)文化 ToString(String)

format參數可以是任何有效的標準數值格式字串,或是自訂數值格式字串的任意組合。 若 format 等 String.Empty 於 或 , null則當前 UInt32 物件的回傳值會以一般格式指定符(“G”)格式化。 如果 format 為任何其他值,方法會擲回 FormatException。

.NET 提供廣泛的格式支援,如下列格式化主題中更詳細地說明:

參數 provider 是一個 IFormatProvider 實作。 其 GetFormat 方法回傳一個 NumberFormatInfo 物件,提供關於該方法回傳字串格式的文化特定資訊。 當 ToString(String, IFormatProvider) 方法被呼叫時,它會呼叫 provider 參數的方法 IFormatProvider.GetFormat ,並傳入 Type 一個代表該型別的 NumberFormatInfo 物件。 接著,方法 GetFormat 回傳 NumberFormatInfo 提供當前值格式 UInt32 化資訊的物件,例如分隔符號或小數點符號。 使用 provider 參數提供格式資訊 ToString(String, IFormatProvider) 有三種方式:

若 provider 是 null,則回傳字串的格式基於 NumberFormatInfo 當前文化的物件。

另請參閱

適用於

ToString()

來源:
UInt32.cs
來源:
UInt32.cs
來源:
UInt32.cs
來源:
UInt32.cs
來源:
UInt32.cs

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

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

傳回

此實例值的字串表示,由一串數字組成,從 0 到 9 不加符號或前置零。

範例

以下範例是使用預設UInt32方法顯示一個ToString()值。 它也會顯示使用某些標準格式指定符所產生的字串表示 UInt32 值。 範例以 en-US 文化的格式慣例呈現。

using System;

public class Example
{
   public static void Main()
   {
      uint value = 1632490;
      // 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,16}", 
                           format, value.ToString(format));         
   }
}
// The example displays the following output:
//       1632490
//       
//       G format specifier:          1632490
//       C format specifier:    $1,632,490.00
//       D format specifier:          1632490
//       F format specifier:       1632490.00
//       N format specifier:     1,632,490.00
//       X format specifier:           18E8EA
let value = 1632490u
// 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,16}"
// The example displays the following output:
//       1632490
//       
//       G format specifier:          1632490
//       C format specifier:    $1,632,490.00
//       D format specifier:          1632490
//       F format specifier:       1632490.00
//       N format specifier:     1,632,490.00
//       X format specifier:           18E8EA
Module Example
   Public Sub Main()
      Dim value As UInteger = 1632490
      ' 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,16}", _
                           format, value.ToString(format))         
      Next
   End Sub
End Module
' The example displays the following output:
'       1632490
'       
'       G format specifier:          1632490
'       C format specifier:    $1,632,490.00
'       D format specifier:          1632490
'       F format specifier:       1632490.00
'       N format specifier:     1,632,490.00
'       X format specifier:           18E8EA

備註

此ToString()方法透過使用UInt32當前文化的物件,將NumberFormatInfo值格式化為預設(「G」或一般格式)。 如果你想指定不同的格式或文化,請使用方法的其他超載 ToString ,如下:

使用格式 為了文化 利用過載
預設(「G」)格式 特定文化 ToString(IFormatProvider)
特定格式 預設(現行)文化 ToString(String)
特定格式 特定文化 ToString(String, IFormatProvider)

另請參閱

適用於