UInt16.ToString メソッド

定義

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

オーバーロード

名前 説明
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 オブジェクトを使用して、既定の ("G"、または一般) 形式でNumberFormatInfo値を書式設定します。 別の形式またはカルチャを指定する場合は、次のように、 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 の範囲の数字のシーケンスで構成され、符号または先頭にゼロがありません。

実装

次の例では、インバリアント カルチャ用の 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 }; 
      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 オブジェクトを使用して、既定の ("G"、または一般) 形式でNumberFormatInfo値を書式設定します。 別の形式または現在のカルチャを指定する場合は、次のように、 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パラメーターには、任意の有効な標準数値書式指定子、またはカスタム数値書式指定子の任意の組み合わせを指定できます。 formatString.Emptyに等しいか、nullされている場合、現在のUInt16 オブジェクトの戻り値は、一般的な書式指定子 ("G") で書式設定されます。 formatが他の値である場合、メソッドはFormatExceptionをスローします。

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

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

こちらもご覧ください

適用対象

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パラメーターには、任意の有効な標準数値書式指定文字列、またはカスタム数値書式指定文字列の任意の組み合わせを指定できます。 formatString.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) メソッドに書式情報を指定するには、次の 3 つの方法があります。

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

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

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

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

こちらもご覧ください

適用対象