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

注解

方法ToString(IFormatProvider)UInt32使用NumberFormatInfo指定区域性的 对象设置默认 (“G”或常规) 格式的值的格式。 如果要指定其他格式或当前区域性,请使用 方法的其他重载 ToString ,如下所示:

使用格式 对于区域性 使用重载
默认 (“G”) 格式 默认 (当前) 区域性 ToString()
特定格式 默认 (当前) 区域性 ToString(String)
特定格式 特定区域性 ToString(String, IFormatProvider)

参数 provider 是实现 IFormatProvider 。 其 GetFormat 方法返回一个 NumberFormatInfo 对象,该对象提供特定于区域性的格式设置信息。 但是,使用常规数值格式说明符 (“G”) 进行格式设置时,不使用 的任何属性 NumberFormatInfo

另请参阅

适用于

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,则使用常规格式说明符 (“G”) 格式化当前 UInt32 对象的返回值。 如果 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)UInt32使用NumberFormatInfo指定区域性的 对象设置指定格式的值的格式。 如果要使用默认格式或区域性设置,请使用 方法的其他重载 ToString ,如下所示:

使用格式 对于区域性 使用重载
默认 (“G”) 格式 默认 (当前) 区域性 ToString()
默认 (“G”) 格式 特定区域性 ToString(IFormatProvider)
特定格式 默认 (当前) 区域性 ToString(String)

参数 format 可以是任何有效的 标准数字格式字符串,也可以是 自定义数字格式字符串的任意组合。 如果 format 等于 String.Empty 或 为 null,则使用常规格式说明符 (“G”) 格式化当前 UInt32 对象的返回值。 如果 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

注解

方法ToString()UInt32使用NumberFormatInfo当前区域性的 对象设置默认 (“G”或常规) 格式的值。 如果要指定不同的格式或区域性,请使用 方法的其他重载 ToString ,如下所示:

使用格式 对于区域性 使用重载
默认 (“G”) 格式 特定区域性 ToString(IFormatProvider)
特定格式 默认 (当前) 区域性 ToString(String)
特定格式 特定区域性 ToString(String, IFormatProvider)

另请参阅

适用于