次の方法で共有


UInt32.ToString メソッド

定義

このインスタンスの数値を等価の文字列形式に変換します。

オーバーロード

ToString()

このインスタンスの数値を等価の文字列形式に変換します。

ToString(IFormatProvider)

指定したカルチャ固有の書式情報を使用して、このインスタンスの数値を等価の文字列形式に変換します。

ToString(String)

指定した形式を使用して、このインスタンスの数値を等価の文字列形式に変換します。

ToString(String, IFormatProvider)

指定した書式とカルチャ固有の書式情報を使用して、このインスタンスの数値を等価の文字列形式に変換します。

ToString()

ソース:
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 までの数字のシーケンスで構成され、符号または先行ゼロはありません。

次の例では、既定の 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() メソッドは、現在のカルチャの NumberFormatInfo オブジェクトを使用して、既定の ("G"、または一般) 形式で UInt32 値を書式設定します。 別の形式またはカルチャを指定する場合は、次のように、ToString メソッドの他のオーバーロードを使用します。

書式を使用するには カルチャの場合 オーバーロードを使用する
既定の ("G") 形式 特定のカルチャ ToString(IFormatProvider)
特定の形式 既定の (現在の) カルチャ ToString(String)
特定の形式 特定のカルチャ ToString(String, IFormatProvider)

こちらもご覧ください

適用対象

ToString(IFormatProvider)

ソース:
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 の範囲の数字のシーケンスで構成され、符号または先頭にゼロがありません。

実装

次の例では、インバリアント カルチャ用の 1 つを含む複数の形式プロバイダーを使用して、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) メソッドは、指定したカルチャの NumberFormatInfo オブジェクトを使用して、既定の ("G"、または一般) 形式で UInt32 値を書式設定します。 別の形式または現在のカルチャを指定する場合は、次のように、ToString メソッドの他のオーバーロードを使用します。

書式を使用するには カルチャの場合 オーバーロードを使用する
既定の ("G") 形式 既定の (現在の) カルチャ ToString()
特定の形式 既定の (現在の) カルチャ ToString(String)
特定の形式 特定のカルチャ ToString(String, IFormatProvider)

provider パラメーターは、IFormatProvider 実装です。 その GetFormat メソッドは、カルチャ固有の書式設定情報を提供する NumberFormatInfo オブジェクトを返します。 ただし、一般的な数値書式指定子 ("G") を使用して書式設定する場合、NumberFormatInfo のプロパティは使用されていません。

こちらもご覧ください

適用対象

ToString(String)

ソース:
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) メソッドは、現在のカルチャの規則を表す NumberFormatInfo オブジェクトを使用して、指定した形式で UInt32 値を書式設定します。 既定の ("G"、または一般) 形式を使用する場合、または別のカルチャを指定する場合は、次のように、ToString メソッドの他のオーバーロードを使用します。

書式を使用するには カルチャの場合 オーバーロードを使用する
既定の ("G") 形式 既定の (現在の) カルチャ ToString()
既定の ("G") 形式 特定のカルチャ ToString(IFormatProvider)
特定の形式 特定のカルチャ ToString(String, IFormatProvider)

パラメーターには、任意の有効な 標準数値書式指定文字列、またはカスタム数値書式指定文字列の任意の組み合わせ 指定できます。 formatString.Empty または nullの場合、現在の UInt32 オブジェクトの戻り値は、一般的な書式指定子 ("G") で書式設定されます。 format が他の値である場合、メソッドは FormatExceptionをスローします。

.NET では、次の書式設定に関するトピックで詳しく説明されている、広範な書式設定のサポートが提供されています。

  • 数値書式指定子の詳細については、「標準の数値書式指定文字列の とカスタム数値書式指定文字列する」を参照してください。

  • .NET での書式設定のサポートの詳細については、「書式設定の種類を参照してください。

返される文字列の形式は、現在のカルチャの NumberFormatInfo オブジェクトによって決まります。 このオブジェクトは、format パラメーターに応じて、グループ区切り記号や出力文字列の小数点記号などのシンボルを制御します。 現在のカルチャ以外のカルチャの書式設定情報を提供するには、ToString(String, IFormatProvider) オーバーロードを呼び出します。

こちらもご覧ください

適用対象

ToString(String, IFormatProvider)

ソース:
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) メソッドは、指定したカルチャの NumberFormatInfo オブジェクトを使用して、指定した形式で UInt32 値を書式設定します。 既定の形式またはカルチャ設定を使用する場合は、次のように、ToString メソッドの他のオーバーロードを使用します。

書式を使用するには カルチャの場合 オーバーロードを使用する
既定の ("G") 形式 既定の (現在の) カルチャ ToString()
既定の ("G") 形式 特定のカルチャ ToString(IFormatProvider)
特定の形式 既定の (現在の) カルチャ ToString(String)

パラメーターには、任意の有効な 標準数値書式指定文字列、またはカスタム数値書式指定文字列の任意の組み合わせ 指定できます。 formatString.Empty または nullの場合、現在の UInt32 オブジェクトの戻り値は、一般的な書式指定子 ("G") で書式設定されます。 format が他の値である場合、メソッドは FormatExceptionをスローします。

.NET では、次の書式設定に関するトピックで詳しく説明されている、広範な書式設定のサポートが提供されています。

  • 数値書式指定子の詳細については、「標準の数値書式指定文字列の とカスタム数値書式指定文字列する」を参照してください。

  • 書式設定の詳細については、「書式設定の種類」を参照してください。

provider パラメーターは、IFormatProvider 実装です。 その GetFormat メソッドは、このメソッドによって返される文字列の形式に関するカルチャ固有の情報を提供する NumberFormatInfo オブジェクトを返します。 ToString(String, IFormatProvider) メソッドが呼び出されると、provider パラメーターの IFormatProvider.GetFormat メソッドが呼び出され、NumberFormatInfo 型を表す Type オブジェクトが渡されます。 GetFormat メソッドは、グループ区切り記号や小数点記号など、現在の UInt32 値を書式設定するための情報を提供する NumberFormatInfo オブジェクトを返します。 provider パラメーターを使用して、ToString(String, IFormatProvider) メソッドに書式情報を指定するには、次の 3 つの方法があります。

  • 書式設定情報を提供するカルチャを表す CultureInfo オブジェクトを渡すことができます。 その GetFormat メソッドは、そのカルチャの数値書式情報を提供する NumberFormatInfo オブジェクトを返します。

  • 数値書式情報を提供する実際の NumberFormatInfo オブジェクトを渡すことができます。 (GetFormat の実装はそれ自体を返すだけです)。

  • IFormatProviderを実装するカスタム オブジェクトを渡すことができます。 その GetFormat メソッドは、書式設定情報を提供する NumberFormatInfo オブジェクトをインスタンス化して返します。

providernull場合、返される文字列の書式設定は、現在のカルチャの NumberFormatInfo オブジェクトに基づきます。

こちらもご覧ください

適用対象