英語で読む

次の方法で共有


Double.ToString メソッド

定義

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

オーバーロード

ToString()

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

ToString(IFormatProvider)

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

ToString(String)

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

ToString(String, IFormatProvider)

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

ToString()

ソース:
Double.cs
ソース:
Double.cs
ソース:
Double.cs

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

C#
public override string ToString ();

戻り値

このインスタンスの値の文字列形式。

次の例では、既定 Double.ToString() のメソッドを使用して、複数の Double 値の文字列表現を表示します。

C#
double number;

number = 1.6E20;
// Displays 1.6E+20.
Console.WriteLine(number.ToString());

number = 1.6E2;
// Displays 160.
Console.WriteLine(number.ToString());

number = -3.541;
// Displays -3.541.
Console.WriteLine(number.ToString());

number = -1502345222199E-07;
// Displays -150234.5222199.
Console.WriteLine(number.ToString());

number = -15023452221990199574E-09;
// Displays -15023452221.9902.
Console.WriteLine(number.ToString());

number = .60344;
// Displays 0.60344.
Console.WriteLine(number.ToString());

number = .000000001;
// Displays 1E-09.
Console.WriteLine(number.ToString());

次の例は、ToStringの使用方法を示しています。

C#
bool done = false;
string inp;
do {
   Console.Write("Enter a real number: ");
   inp = Console.ReadLine();
   try {
      d = Double.Parse(inp);
      Console.WriteLine("You entered {0}.", d.ToString());
      done = true;
   }
   catch (FormatException) {
      Console.WriteLine("You did not enter a number.");
   }
   catch (ArgumentNullException) {
      Console.WriteLine("You did not supply any input.");
   }
   catch (OverflowException) {
       Console.WriteLine("The value you entered, {0}, is out of range.", inp);
   }
} while (!done);

注釈

メソッドは ToString() 、現在の Double カルチャの既定の ("G"、または一般的な) 形式で値を書式設定します。 別の形式、有効桁数、またはカルチャを指定する場合は、次のように メソッドの他のオーバーロードを ToString 使用します。

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

戻り値には PositiveInfinitySymbol、、 NegativeInfinitySymbolNaNSymbol、または 形式の文字列を指定できます。

[sign]integral-digits[.[fractional-digits]][E[sign]exponential-digits]

省略可能な要素は、角かっこ ([ と ]) で囲まれます。 "digits" という用語を含む要素は、0 から 9 までの一連の数字で構成されます。 次の表に示す要素がサポートされています。

要素 説明
sign 負符号または正符号記号。
整数桁 数値の整数部分を指定する一連の数字。 小数部が存在する場合、整数桁は存在しない可能性があります。
'.' カルチャ固有の小数点記号。
小数部の桁数 数値の小数部を指定する一連の数字。
'E' 指数 (指数) 表記を示す大文字の 'E'。
exponential-digits 指数を指定する一連の数字。

戻り値の例としては、"100"、"-123,456,789"、"123.45E+6"、"500"、"3.1416"、"600"、"-0.123"、"-Infinity" があります。

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

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToString(IFormatProvider)

ソース:
Double.cs
ソース:
Double.cs
ソース:
Double.cs

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

C#
public string ToString (IFormatProvider provider);
C#
public string ToString (IFormatProvider? provider);

パラメーター

provider
IFormatProvider

カルチャ固有の書式情報を提供するオブジェクト。

戻り値

provider で指定された、このインスタンスの値の文字列形式。

実装

次の例では、複数の異なるカルチャを表す オブジェクトを使用してCultureInfo、2 つのDouble値の文字列表現を表示します。

C#
double value;

value = -16325.62015;
// Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture));
// Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")));
// Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")));

value = 16034.125E21;
// Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture));
// Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")));
// Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")));
// This example displays the following output to the console:
//       -16325.62015
//       -16325.62015
//       -16325,62015
//       1.6034125E+25
//       1.6034125E+25
//       1,6034125E+25

次の例は、 パラメーターとして と を受け取る String の使用方法ToStringIFormatProvider示しています。

C#
public class Temperature : IFormattable {
    // IFormattable.ToString implementation.
    public string ToString(string format, IFormatProvider provider) {
        if( format != null ) {
            if( format.Equals("F") ) {
                return String.Format("{0}'F", this.Value.ToString());
            }
            if( format.Equals("C") ) {
                return String.Format("{0}'C", this.Celsius.ToString());
            }
        }

        return m_value.ToString(format, provider);
    }

    // The value holder
    protected double m_value;

    public double Value {
        get {
            return m_value;
        }
        set {
            m_value = value;
        }
    }

    public double Celsius {
        get {
            return (m_value-32.0)/1.8;
        }
        set {
            m_value = 1.8*value+32.0;
        }
    }
}

注釈

メソッドは ToString(IFormatProvider)Double 指定したカルチャの既定の ("G"、または一般的な) 形式で値を書式設定します。 別の形式またはカルチャを指定する場合は、次のように メソッドの他のオーバーロードを ToString 使用します。

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

戻り値には PositiveInfinitySymbol、、 NegativeInfinitySymbolNaNSymbol、または 形式の文字列を指定できます。

[sign]integral-digits[.[fractional-digits]][E[sign]exponential-digits]

省略可能な要素は、角かっこ ([ と ]) で囲まれます。 "digits" という用語を含む要素は、0 から 9 までの一連の数字で構成されます。 次の表に示す要素がサポートされています。

要素 説明
sign 負符号または正符号記号。
整数桁 数値の整数部分を指定する一連の数字。 小数部が存在する場合、整数桁は存在しない可能性があります。
'.' カルチャ固有の小数点記号。
小数部の桁数 数値の小数部を指定する一連の数字。
'E' 指数 (指数) 表記を示す大文字の 'E'。
exponential-digits 指数を指定する一連の数字。

戻り値の例としては、"100"、"-123,456,789"、"123.45E+6"、"500"、"3.1416"、"600"、"-0.123"、"-Infinity" があります。

このインスタンスは、一般的な数値書式指定子 ("G") で書式設定されます。

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

パラメーターは provider 、メソッドが IFormatProvider オブジェクトを GetFormat 返す実装です NumberFormatInfo 。 通常、 providerCultureInfo オブジェクトまたは オブジェクトです NumberFormatInfo 。 パラメーターは provider 、書式設定で使用されるカルチャ固有の情報を提供します。 が nullの場合provider、戻り値は現在のカルチャの オブジェクトをNumberFormatInfo使用して書式設定されます。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToString(String)

ソース:
Double.cs
ソース:
Double.cs
ソース:
Double.cs

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

C#
public string ToString (string format);
C#
public string ToString (string? format);

パラメーター

format
String

数値書式指定文字列。

戻り値

format で指定された、このインスタンスの値の文字列形式。

例外

format が無効です。

次の例では、数値を定義し、"C" 標準の数値書式指定文字列を使用して通貨値として書式設定し、"N" 標準の数値書式指定文字列を使用して小数点以下 3 桁の数値として書式設定します。 結果文字列は、en-US カルチャの規則を使用して書式設定されます。 数値書式指定文字列の詳細については、「 標準の数値書式指定文字列 」および 「カスタム数値書式指定文字列」を参照してください。

C#
using System;

public class Example
{
   public static void Main()
   {
      float number = 1764.3789m;

      // Format as a currency value.
      Console.WriteLine(number.ToString("C"));

      // Format as a numeric value with 3 decimal places.
      Console.WriteLine(number.ToString("N3"));
   }
}
// The example displays the following output:
//       $1,764.38
//       1,764.379

次の例では、サポートされている標準の数値書式指定子と 3 つのカスタム数値書式指定文字列を使用して、いくつかの Double 値を表示します。 これらのカスタム書式指定文字列の 1 つは、先頭に 0 を付けて値を Single 埋め込む方法を示しています。 さらに、この例では、"R" を除く各標準書式指定子で有効桁数指定子を使用します。 精度指定子の値の範囲は 0 から 3 です。 この例では、数値を文字列に変換するために、en-US カルチャの書式設定規則を使用します。

C#
double[] numbers= {1054.32179, -195489100.8377, 1.0437E21,
                   -1.0573e-05};
string[] specifiers = { "C", "E", "e", "F", "G", "N", "P",
                        "R", "#,000.000", "0.###E-000",
                        "000,000,000,000.00###" };
foreach (double number in numbers)
{
   Console.WriteLine("Formatting of {0}:", number);
   foreach (string specifier in specifiers) {
      Console.WriteLine("   {0,-22} {1}",
                        specifier + ":", number.ToString(specifier));
      // Add precision specifiers from 0 to 3.
      if (specifier.Length == 1 & ! specifier.Equals("R")) {
         for (int precision = 0; precision <= 3; precision++) {
            string pSpecifier = String.Format("{0}{1}", specifier, precision);
            Console.WriteLine("   {0,-22} {1}",
                              pSpecifier + ":", number.ToString(pSpecifier));
         }
         Console.WriteLine();
      }
   }
   Console.WriteLine();
}
// The example displays the following output to the console:
//       Formatting of 1054.32179:
//          C:                     $1,054.32
//          C0:                    $1,054
//          C1:                    $1,054.3
//          C2:                    $1,054.32
//          C3:                    $1,054.322
//
//          E:                     1.054322E+003
//          E0:                    1E+003
//          E1:                    1.1E+003
//          E2:                    1.05E+003
//          E3:                    1.054E+003
//
//          e:                     1.054322e+003
//          e0:                    1e+003
//          e1:                    1.1e+003
//          e2:                    1.05e+003
//          e3:                    1.054e+003
//
//          F:                     1054.32
//          F0:                    1054
//          F1:                    1054.3
//          F2:                    1054.32
//          F3:                    1054.322
//
//          G:                     1054.32179
//          G0:                    1054.32179
//          G1:                    1E+03
//          G2:                    1.1E+03
//          G3:                    1.05E+03
//
//          N:                     1,054.32
//          N0:                    1,054
//          N1:                    1,054.3
//          N2:                    1,054.32
//          N3:                    1,054.322
//
//          P:                     105,432.18 %
//          P0:                    105,432 %
//          P1:                    105,432.2 %
//          P2:                    105,432.18 %
//          P3:                    105,432.179 %
//
//          R:                     1054.32179
//          #,000.000:             1,054.322
//          0.###E-000:            1.054E003
//          000,000,000,000.00###: 000,000,001,054.32179
//
//       Formatting of -195489100.8377:
//          C:                     ($195,489,100.84)
//          C0:                    ($195,489,101)
//          C1:                    ($195,489,100.8)
//          C2:                    ($195,489,100.84)
//          C3:                    ($195,489,100.838)
//
//          E:                     -1.954891E+008
//          E0:                    -2E+008
//          E1:                    -2.0E+008
//          E2:                    -1.95E+008
//          E3:                    -1.955E+008
//
//          e:                     -1.954891e+008
//          e0:                    -2e+008
//          e1:                    -2.0e+008
//          e2:                    -1.95e+008
//          e3:                    -1.955e+008
//
//          F:                     -195489100.84
//          F0:                    -195489101
//          F1:                    -195489100.8
//          F2:                    -195489100.84
//          F3:                    -195489100.838
//
//          G:                     -195489100.8377
//          G0:                    -195489100.8377
//          G1:                    -2E+08
//          G2:                    -2E+08
//          G3:                    -1.95E+08
//
//          N:                     -195,489,100.84
//          N0:                    -195,489,101
//          N1:                    -195,489,100.8
//          N2:                    -195,489,100.84
//          N3:                    -195,489,100.838
//
//          P:                     -19,548,910,083.77 %
//          P0:                    -19,548,910,084 %
//          P1:                    -19,548,910,083.8 %
//          P2:                    -19,548,910,083.77 %
//          P3:                    -19,548,910,083.770 %
//
//          R:                     -195489100.8377
//          #,000.000:             -195,489,100.838
//          0.###E-000:            -1.955E008
//          000,000,000,000.00###: -000,195,489,100.8377
//
//       Formatting of 1.0437E+21:
//          C:                     $1,043,700,000,000,000,000,000.00
//          C0:                    $1,043,700,000,000,000,000,000
//          C1:                    $1,043,700,000,000,000,000,000.0
//          C2:                    $1,043,700,000,000,000,000,000.00
//          C3:                    $1,043,700,000,000,000,000,000.000
//
//          E:                     1.043700E+021
//          E0:                    1E+021
//          E1:                    1.0E+021
//          E2:                    1.04E+021
//          E3:                    1.044E+021
//
//          e:                     1.043700e+021
//          e0:                    1e+021
//          e1:                    1.0e+021
//          e2:                    1.04e+021
//          e3:                    1.044e+021
//
//          F:                     1043700000000000000000.00
//          F0:                    1043700000000000000000
//          F1:                    1043700000000000000000.0
//          F2:                    1043700000000000000000.00
//          F3:                    1043700000000000000000.000
//
//          G:                     1.0437E+21
//          G0:                    1.0437E+21
//          G1:                    1E+21
//          G2:                    1E+21
//          G3:                    1.04E+21
//
//          N:                     1,043,700,000,000,000,000,000.00
//          N0:                    1,043,700,000,000,000,000,000
//          N1:                    1,043,700,000,000,000,000,000.0
//          N2:                    1,043,700,000,000,000,000,000.00
//          N3:                    1,043,700,000,000,000,000,000.000
//
//          P:                     104,370,000,000,000,000,000,000.00 %
//          P0:                    104,370,000,000,000,000,000,000 %
//          P1:                    104,370,000,000,000,000,000,000.0 %
//          P2:                    104,370,000,000,000,000,000,000.00 %
//          P3:                    104,370,000,000,000,000,000,000.000 %
//
//          R:                     1.0437E+21
//          #,000.000:             1,043,700,000,000,000,000,000.000
//          0.###E-000:            1.044E021
//          000,000,000,000.00###: 1,043,700,000,000,000,000,000.00
//
//       Formatting of -1.0573E-05:
//          C:                     $0.00
//          C0:                    $0
//          C1:                    $0.0
//          C2:                    $0.00
//          C3:                    $0.000
//
//          E:                     -1.057300E-005
//          E0:                    -1E-005
//          E1:                    -1.1E-005
//          E2:                    -1.06E-005
//          E3:                    -1.057E-005
//
//          e:                     -1.057300e-005
//          e0:                    -1e-005
//          e1:                    -1.1e-005
//          e2:                    -1.06e-005
//          e3:                    -1.057e-005
//
//          F:                     0.00
//          F0:                    0
//          F1:                    0.0
//          F2:                    0.00
//          F3:                    0.000
//
//          G:                     -1.0573E-05
//          G0:                    -1.0573E-05
//          G1:                    -1E-05
//          G2:                    -1.1E-05
//          G3:                    -1.06E-05
//
//          N:                     0.00
//          N0:                    0
//          N1:                    0.0
//          N2:                    0.00
//          N3:                    0.000
//
//          P:                     0.00 %
//          P0:                    0 %
//          P1:                    0.0 %
//          P2:                    0.00 %
//          P3:                    -0.001 %
//
//          R:                     -1.0573E-05
//          #,000.000:             000.000
//          0.###E-000:            -1.057E-005
//          000,000,000,000.00###: -000,000,000,000.00001

注釈

メソッドは ToString(String) 、現在の Double カルチャの規則を使用して、指定した形式で値を書式設定します。 別の形式またはカルチャを指定する場合は、次のように メソッドの他のオーバーロードを ToString 使用します。

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

戻り値には、 PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbol、または で指定された数値の文字列表現を format指定できます。

パラメーターには format 、D と X を除く任意の有効な標準数値書式指定子と、カスタム数値書式指定子の任意の組み合わせを指定できます。 が null または空の文字列の場合format、戻り値は一般的な数値書式指定子 ("G") で書式設定されます。

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

既定では、戻り値には 15 桁の有効桁数のみが含まれますが、内部的には最大 17 桁が維持されます。 このインスタンスの値が 15 桁を超える場合は、ToString期待される数値の代わりに または NegativeInfinitySymbol を返PositiveInfinitySymbolします。 より多くの精度が必要な場合は、常に 17 桁の有効桁数を返す "G17" 形式の仕様を使用して を指定するか、"R" を指定 format します。これは、数値をその有効桁数で表すことができる場合は 15 桁、最大有効桁数でのみ表すことができる場合は 17 桁を返します。

注意 (呼び出し元)

場合によっては、/platform:x64 スイッチまたは /platform:anycpu スイッチを使用してコンパイルして 64 ビット システムで実行すると、"R" 標準の数値書式指定文字列で書式設定される Double 値のラウンドトリップに失敗することがあります。 この問題を回避するには、"G17" 標準の数値書式指定文字列を使用して Double 値を書式設定することができます。 次の例では、ラウンドトリップに失敗する Double 値を持つ "R" 書式指定文字列を使用しています。元の値のラウンドトリップに成功する "G17" 書式指定文字列も使用しています。

C#
using System;

public class Example
{
   static void Main(string[] args)
   {
      Console.WriteLine("Attempting to round-trip a Double with 'R':");
      double initialValue = 0.6822871999174;
      string valueString = initialValue.ToString("R");
      double roundTripped = double.Parse(valueString);
      Console.WriteLine("{0:R} = {1:R}: {2}\n",
                        initialValue, roundTripped, initialValue.Equals(roundTripped));

      Console.WriteLine("Attempting to round-trip a Double with 'G17':");
      string valueString17 = initialValue.ToString("G17");
      double roundTripped17 = double.Parse(valueString17);
      Console.WriteLine("{0:R} = {1:R}: {2}\n",
                        initialValue, roundTripped17, initialValue.Equals(roundTripped17));
   }
}
// If compiled to an application that targets anycpu or x64 and run on an x64 system,
// the example displays the following output:
//       Attempting to round-trip a Double with 'R':
//       0.6822871999174 = 0.68228719991740006: False
//
//       Attempting to round-trip a Double with 'G17':
//       0.6822871999174 = 0.6822871999174: True

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToString(String, IFormatProvider)

ソース:
Double.cs
ソース:
Double.cs
ソース:
Double.cs

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

C#
public string ToString (string format, IFormatProvider provider);
C#
public string ToString (string? format, IFormatProvider? provider);

パラメーター

format
String

数値書式指定文字列。

provider
IFormatProvider

カルチャ固有の書式情報を提供するオブジェクト。

戻り値

format および provider で指定された、このインスタンスの値の文字列形式。

実装

次の例では、 Double 複数の異なるカルチャでサポートされている各標準数値書式指定子を使用して値を表示します。

C#
double value = 16325.62901;
string specifier;
CultureInfo culture;

// Use standard numeric format specifiers.
specifier = "G";
culture = CultureInfo.CreateSpecificCulture("eu-ES");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16325,62901
Console.WriteLine(value.ToString(specifier, CultureInfo.InvariantCulture));
// Displays:    16325.62901

specifier = "C";
culture = CultureInfo.CreateSpecificCulture("en-US");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    $16,325.63
culture = CultureInfo.CreateSpecificCulture("en-GB");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    £16,325.63

specifier = "E04";
culture = CultureInfo.CreateSpecificCulture("sv-SE");
Console.WriteLine(value.ToString(specifier, culture));
// Displays: 1,6326E+004
 culture = CultureInfo.CreateSpecificCulture("en-NZ");
 Console.WriteLine(value.ToString(specifier, culture));
// Displays:    1.6326E+004

specifier = "F";
culture = CultureInfo.CreateSpecificCulture("fr-FR");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16325,63
culture = CultureInfo.CreateSpecificCulture("en-CA");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16325.63

specifier = "N";
culture = CultureInfo.CreateSpecificCulture("es-ES");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16.325,63
culture = CultureInfo.CreateSpecificCulture("fr-CA");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16 325,63

specifier = "P";
culture = CultureInfo.InvariantCulture;
Console.WriteLine((value/10000).ToString(specifier, culture));
// Displays:    163.26 %
culture = CultureInfo.CreateSpecificCulture("ar-EG");
Console.WriteLine((value/10000).ToString(specifier, culture));
// Displays:    163.256 %

次の例は、 パラメーターとして と を受け取る String の使用方法ToStringIFormatProvider示しています。

C#
public class Temperature : IFormattable {
    // IFormattable.ToString implementation.
    public string ToString(string format, IFormatProvider provider) {
        if( format != null ) {
            if( format.Equals("F") ) {
                return String.Format("{0}'F", this.Value.ToString());
            }
            if( format.Equals("C") ) {
                return String.Format("{0}'C", this.Celsius.ToString());
            }
        }

        return m_value.ToString(format, provider);
    }

    // The value holder
    protected double m_value;

    public double Value {
        get {
            return m_value;
        }
        set {
            m_value = value;
        }
    }

    public double Celsius {
        get {
            return (m_value-32.0)/1.8;
        }
        set {
            m_value = 1.8*value+32.0;
        }
    }
}

注釈

メソッドは ToString(String, IFormatProvider)Double 指定したカルチャの指定した形式で値を書式設定します。 別の形式またはカルチャを指定する場合は、次のように メソッドの他のオーバーロードを ToString 使用します。

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

戻り値には、 PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbol、または で指定された数値の文字列表現を format指定できます。

パラメーターには format 、D と X を除く任意の有効な標準数値書式指定子と、カスタム数値書式指定子の任意の組み合わせを指定できます。 が null または空の文字列の場合format、このインスタンスの戻り値は一般的な数値書式指定子 ("G") で書式設定されます。

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

パラメーターは provider 、メソッドが IFormatProvider オブジェクトを GetFormat 返す実装です NumberFormatInfo 。 通常、 providerCultureInfo オブジェクトまたは オブジェクトです NumberFormatInfo 。 パラメーターは provider 、書式設定で使用されるカルチャ固有の情報を提供します。 が nullの場合provider、戻り値は現在のカルチャの オブジェクトをNumberFormatInfo使用して書式設定されます。

既定では、戻り値には 15 桁の有効桁数のみが含まれますが、内部的には最大 17 桁が維持されます。 このインスタンスの値が 15 桁を超える場合は、ToString期待される数値の代わりに または NegativeInfinitySymbol を返PositiveInfinitySymbolします。 より多くの精度が必要な場合は、常に 17 桁の有効桁数を返す "G17" 形式の仕様を使用して を指定するか、"R" を指定 format します。これは、数値をその有効桁数で表すことができる場合は 15 桁、最大有効桁数でのみ表すことができる場合は 17 桁を返します。

注意 (呼び出し元)

場合によっては、/platform:x64 スイッチまたは /platform:anycpu スイッチを使用してコンパイルして 64 ビット システムで実行すると、"R" 標準の数値書式指定文字列で書式設定される Double 値のラウンドトリップに失敗することがあります。 この問題を回避するには、"G17" 標準の数値書式指定文字列を使用して Double 値を書式設定することができます。 次の例では、ラウンドトリップに失敗する Double 値を持つ "R" 書式指定文字列を使用しています。元の値のラウンドトリップに成功する "G17" 書式指定文字列も使用しています。

C#
using System;
using System.Globalization;

public class Example
{
   static void Main(string[] args)
   {
      Console.WriteLine("Attempting to round-trip a Double with 'R':");
      double initialValue = 0.6822871999174;
      string valueString = initialValue.ToString("R",
                                                 CultureInfo.InvariantCulture);
      double roundTripped = double.Parse(valueString,
                                         CultureInfo.InvariantCulture);
      Console.WriteLine("{0:R} = {1:R}: {2}\n",
                        initialValue, roundTripped, initialValue.Equals(roundTripped));

      Console.WriteLine("Attempting to round-trip a Double with 'G17':");
      string valueString17 = initialValue.ToString("G17",
                                                   CultureInfo.InvariantCulture);
      double roundTripped17 = double.Parse(valueString17,
                                           CultureInfo.InvariantCulture);
      Console.WriteLine("{0:R} = {1:R}: {2}\n",
                        initialValue, roundTripped17, initialValue.Equals(roundTripped17));
      // If compiled to an application that targets anycpu or x64 and run on an x64 system,
      // the example displays the following output:
      //       Attempting to round-trip a Double with 'R':
      //       0.6822871999174 = 0.68228719991740006: False
      //
      //       Attempting to round-trip a Double with 'G17':
      //       0.6822871999174 = 0.6822871999174: True
   }
}

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0