UInt32.ToString 方法

定義

將這個執行個體的數值轉換為其相等字串表示。

多載

ToString(IFormatProvider)

使用指定的特定文化特性格式資訊,將這個執行個體的數值轉換成它的相等字串表示。

ToString(String)

使用指定的格式,將這個執行個體的數值轉換成它的相等字串表示。

ToString(String, IFormatProvider)

使用指定的格式和特定文化特性格式資訊,將這個執行個體的數值轉換成它的相等字串表示。

ToString()

將這個執行個體的數值轉換為其相等字串表示。

ToString(IFormatProvider)

Source:
UInt32.cs
Source:
UInt32.cs
Source:
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

備註

方法 UInt32ToString(IFormatProvider) 使用 NumberFormatInfo 指定文化特性的 物件,格式化預設 (「G」 或一般) 格式的值。 如果您想要指定不同的格式或目前的文化特性,請使用 方法的其他多載 ToString ,如下所示:

使用格式 針對文化特性 使用多載
預設 (「G」) 格式 預設 (目前) 文化特性 ToString()
特定格式 預設 (目前) 文化特性 ToString(String)
特定格式 特定文化特性 ToString(String, IFormatProvider)

參數 provider 是實作 IFormatProvider 。 其 GetFormat 方法會傳回 提供 NumberFormatInfo 特定文化特性格式資訊的 物件。 不過,使用一般數值格式標準格式化時,不會使用 的屬性 NumberFormatInfo (「G」) 。

另請參閱

適用於

ToString(String)

Source:
UInt32.cs
Source:
UInt32.cs
Source:
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) 會使用 NumberFormatInfo 代表目前文化特性慣例的 物件,以指定格式格式化 UInt32 值。 如果您想要使用預設 (「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) 多載。

另請參閱

適用於

ToString(String, IFormatProvider)

Source:
UInt32.cs
Source:
UInt32.cs
Source:
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

物件,提供關於這個執行個體的文化特性格式資訊。

傳回

這個執行個體值的字串表示,如同 formatprovider 所指定。

實作

例外狀況

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) 會使用 NumberFormatInfo 指定文化特性的 物件,以指定格式格式化 UInt32 值。 如果您想要使用預設格式或文化特性設定,請使用 方法的其他多載 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) 提供給 方法:

如果 為 providernull ,則傳回字串的格式會以目前文化特性的物件為基礎 NumberFormatInfo

另請參閱

適用於

ToString()

Source:
UInt32.cs
Source:
UInt32.cs
Source:
UInt32.cs

將這個執行個體的數值轉換為其相等字串表示。

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

傳回

這個執行個體值的字串表示,由範圍從 0 到 9,沒有正負號或為零的前置字元的數字順序所組成。

範例

下列範例會使用預設 ToString() 方法顯示 UInt32 值。 它也會顯示使用某些標準格式規範所產生的值字串表示 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

備註

方法 UInt32ToString() 使用 NumberFormatInfo 目前文化特性的物件,格式化預設 (「G」 中的值,或一般) 格式。 如果您想要指定不同的格式或文化特性,請使用 方法的其他多載 ToString ,如下所示:

使用格式 針對文化特性 使用多載
預設 (「G」) 格式 特定文化特性 ToString(IFormatProvider)
特定格式 預設 (目前) 文化特性 ToString(String)
特定格式 特定文化特性 ToString(String, IFormatProvider)

另請參閱

適用於