標準の数値書式指定文字列

一般的な数値型を書式設定するには、標準の数値書式指定文字列を使用します。標準の数値書式指定文字列は、Axx という形式を使用します。A は書式指定子と呼ばれる英文字、xx は精度指定子と呼ばれるオプションの整数です。精度指定子は 0 ~ 99 の範囲で指定され、結果の桁数に影響します。空白を含む複数の英文字で構成される数値書式指定文字列は、カスタム数値書式指定文字列として解釈されます。

標準の数値書式指定子を次の表に示します。各書式指定子によって生成される出力の例については、「標準の数値書式指定文字列の出力例」を参照してください。詳細については、表の後の「メモ」を参照してください。

書式指定子 名前 説明

C または c

通貨

数値は、金額を表す文字列に変換されます。変換は、現在の NumberFormatInfo オブジェクトの通貨書式情報によって制御されます。

精度指定子は、小数部の桁数を示します。精度指定子を省略すると、現在の NumberFormatInfo オブジェクトによって指定される既定の金額桁数が使用されます。

D または d

10 進数

この書式指定は整数型でだけサポートされています。数値は、0 ~ 9 の数字から成る文字列に変換されます。負の数値の場合は、文字列の先頭にマイナス記号が挿入されます。

精度指定子は、変換後の文字列の最小桁数を示します。必要に応じて、精度指定子によって指定された桁数に達するまで、数値の左側にゼロが埋め込まれます。

E または e

指数

数値は、"-d.ddd…E+ddd" または "-d.ddd…e+ddd" という形式の文字列に変換されます。この 'd' は 0 ~ 9 までの 1 桁の数字を示します。負の数値の場合、変換後の文字列の先頭にマイナス記号が挿入されます。小数点の前には 1 桁の数字が必ず示されます。

精度指定子は、小数部の桁数を示します。精度指定子を省略すると、小数部の桁数として既定の 6 桁が使用されます。

書式指定子が大文字の場合は指数部の前に 'E' が挿入され、小文字の場合は 'e' が挿入されます。指数部は常に、プラス記号またはマイナス記号のいずれかと、3 桁以上の桁で構成されます。指数部の桁数が最小桁数の 3 桁よりも少ない場合には、3 桁になるようにゼロが埋め込まれます。

F または f

固定小数点

数値は、"-ddd.ddd..." という形式の文字列に変換されます。この 'd' は 0 ~ 9 までの数字を示します。負の数値の場合、変換後の文字列の先頭にマイナス記号が挿入されます。

精度指定子は、小数部の桁数を示します。精度指定子を省略すると、現在の NumberFormatInfo オブジェクトによって指定される既定の桁数が使用されます。

G または g

一般

数値は、数値の型や、精度指定子が指定されているかどうかに応じて、固定小数点表記または指数表記のいずれかの最も簡潔な形式に変換されます。精度指定子が省略されている場合や、0 である場合は、次のリストに示すように、数値の型によって既定の精度が決定されます。

  • Byte または SByte: 3

  • Int16 または UInt16: 5

  • Int32 または UInt32: 10

  • Int64 または UInt64: 19

  • Single: 7

  • Double: 15

  • Decimal: 29

数値を指数表記で表した結果の指数部が -5 よりも大きく、精度指定子よりも小さい場合は、固定小数点表記が使用されます。それ以外の場合は、指数表記が使用されます。結果には必要に応じて小数点が含まれ、後続のゼロは省略されます。精度指定子が存在し、指定された精度を結果の有効桁数が超える場合は、超過した桁は丸められ削除されます。

上記の規則の例外として、数値が Decimal 型である場合は精度指定子は省略されます。この場合は、常に固定小数点表記が使用され、後続のゼロは保持されます。

指数表記が使用される場合、結果の指数部には、書式指定子が 'G' のときには 'E'、書式指定子が 'g' のときには 'e' というプリフィックスが付きます。

N または n

数値

数値は、"-d,ddd,ddd.ddd…" という形式の文字列に変換されます。"-" は負数記号を示し (必要な場合)、"d" は数字 (0 ~ 9) を示し、"," は数値グループ間の桁区切り記号を示し、"." は小数点記号を示します。実際の負数のパターン、数値グループのサイズ、桁区切り記号、および小数点記号は、現在の NumberFormatInfo オブジェクトによって指定されます。

精度指定子は、小数部の桁数を示します。精度指定子を省略すると、現在の NumberFormatInfo オブジェクトによって指定される既定の桁数が使用されます。

P または p

パーセント

数値は、パーセント値を表す文字列に変換されます。この変換は、NumberFormatInfo.PercentNegativePattern プロパティ (数値が負数の場合) または NumberFormatInfo.PercentPositivePattern プロパティ (数値が正数の場合) で定義されます。パーセント値を示すため、変換後の数値に 100 が乗算されます。

精度指定子は、小数部の桁数を示します。精度指定子を省略すると、現在の NumberFormatInfo オブジェクトによって指定される既定の桁数が使用されます。

R または r

ラウンドトリップ

この書式指定は、Single 型および Double 型でだけサポートされています。ラウンドトリップ指定子は、数値の変換後の文字列が、変換前の数値へ戻るように解析されることを指定します。この指定子を使用して数値の書式を設定すると、最初に一般書式を使用して数値がテストされます。このとき、Double の場合は 15 桁、Single の場合は 7 桁の空白が使用されます。変換後の文字列を解析して変換前の数値へ戻った場合には、一般書式指定子を使用してこの数値の書式が設定されます。変換後の文字列が解析によって変換前の数値に戻らなかった場合には、Double の場合は 17 桁、Single の場合は 9 桁を使用してこの値の書式が設定されます。

精度指定子は、指定できますが無視されます。ラウンドトリップ指定子と精度指定子の両方を指定すると、ラウンドトリップ指定子が優先されます。

X または x

16 進数

この書式指定は整数型でだけサポートされています。数値は 16 進数文字列に変換されます。書式指定子の大文字と小文字によって、9 よりも大きい 16 進数値を示すアルファベット文字が大文字と小文字のどちらで表示されるかが決まります。たとえば、'X' を指定すると "ABCDEF" となり、'x' を指定すると "abcdef" となります。

精度指定子は、変換後の文字列の最小桁数を示します。必要に応じて、精度指定子によって指定された桁数に達するまで、数値の左側にゼロが埋め込まれます。

その他の 1 文字

(未定義の指定子)

(未定義の指定子は、ランタイム書式指定例外をスローします。)

メモ

コントロール パネルの設定

コントロール パネルの [地域と言語のオプション] での設定は、書式設定操作によって生成される結果の文字列に影響します。これらの設定は、現在のスレッド カルチャに関連付けられた NumberFormatInfo オブジェクトを初期化するために使用され、現在のスレッド カルチャから書式設定の制御に使用される値が提供されます。コンピュータで使用する設定が異なる場合は、生成される文字列も異なります。

NumberFormatInfo のプロパティ

書式設定は、現在の NumberFormatInfo オブジェクトのプロパティの影響を受けます。このオブジェクトは、現在のスレッド カルチャによって暗黙的に指定されるか、または書式設定を実行するメソッドの IFormatProvider パラメータによって明示的に指定されます。このパラメータには、NumberFormatInfo オブジェクトまたは CultureInfo オブジェクトを指定してください。

整数数値型と浮動小数点数値型

標準の数値書式指定子の記述で、整数数値型または浮動小数点数値型が参照されている場合があります。整数数値型には、ByteSByteInt16Int32Int64UInt16UInt32、および UInt64 があります。浮動小数点数値型には、DecimalSingle、および Double があります。

浮動小数点の無限大値と NaN (非数) 値

Single の浮動小数点型または Double の浮動小数点型が正の無限大、負の無限大、または NaN (非数) である場合は、書式指定文字列とは関係なく、現在適用可能な NumberFormatInfo オブジェクトによって指定される PositiveInfinitySymbolNegativeInfinitySymbol、または NaNSymbol の各プロパティの値が、書式設定された文字列となることに注意してください。

スレッドの現在のカルチャ、指定したカルチャ、およびすべての標準数値書式指定子を使用して、整数値と浮動小数点数値を書式設定するコード例を次に示します。このコード例は、2 つの特定の数値型を使用していますが、どの数値基本型に対しても同じ結果を生成します (数値基本型とは、ByteSByteInt16Int32Int64UInt16UInt32UInt64DecimalSingle、および Double です)。

' This code example demonstrates the ToString(String) and 
' ToString(String, IFormatProvider) methods for integral and
' floating-point numbers, in conjunction with the standard 
' numeric format specifiers.
' This code example uses the System.Int32 integral type and 
' the System.Double floating-point type, but would yield 
' similar results for any of the numeric types. The integral 
' numeric types are System.Byte, SByte, Int16, Int32, Int64, 
' UInt16, UInt32, and UInt64. The floating-point numeric types 
' are Decimal, Single, and Double.

Imports System
Imports System.Globalization
Imports System.Threading

Class Sample
    Public Shared Sub Main() 
        ' Format a negative integer or floating-point number in various ways.
        Dim integralVal As Integer = -12345
        Dim floatingVal As Double  = -1234.567
        
        Dim msgCurrency As String =    "(C) Currency: . . . . . . "
        Dim msgDecimal As String =     "(D) Decimal:. . . . . . . "
        Dim msgScientific As String =  "(E) Scientific: . . . . . "
        Dim msgFixedPoint As String =  "(F) Fixed point:. . . . . "
        Dim msgGeneral As String =     "(G) General (default):. . "
        Dim msgNumber As String =      "(N) Number: . . . . . . . "
        Dim msgPercent As String =     "(P) Percent:. . . . . . . "
        Dim msgRoundTrip As String =   "(R) Round-trip: . . . . . "
        Dim msgHexadecimal As String = "(X) Hexadecimal:. . . . . "
        
        Dim msg1 As String = "Use ToString(String) and the current thread culture." + vbLf
        Dim msg2 As String = "Use ToString(String, IFormatProvider) and a specified culture." + vbLf
        Dim msgCulture As String = "Culture:"
        Dim msgIntegralVal As String = "Integral value:"
        Dim msgFloatingVal As String = "Floating-point value:"
        
        Dim ci As CultureInfo
        '
        Console.Clear()
        Console.WriteLine("Standard Numeric Format Specifiers:" & vbLf)
        ' Display the values.
        Console.WriteLine(msg1)
        
        ' Display the thread current culture, which is used to format the values.
        ci = Thread.CurrentThread.CurrentCulture
        Console.WriteLine("{0,-26}{1}", msgCulture, ci.DisplayName)
        
        ' Display the integral and floating-point values.
        Console.WriteLine("{0,-26}{1}", msgIntegralVal, integralVal)
        Console.WriteLine("{0,-26}{1}", msgFloatingVal, floatingVal)
        Console.WriteLine()
        
        ' Use the format specifiers that are only for integral types.
        Console.WriteLine("Format specifiers only for integral types:")
        Console.WriteLine(msgDecimal     & integralVal.ToString("D"))
        Console.WriteLine(msgHexadecimal & integralVal.ToString("X"))
        Console.WriteLine()
        
        ' Use the format specifier that is only for the Single and Double 
        ' floating-point types.
        Console.WriteLine("Format specifier only for the Single and Double types:")
        Console.WriteLine(msgRoundTrip   & floatingVal.ToString("R"))
        Console.WriteLine()
        
        ' Use the format specifiers that are for integral or floating-point types.
        Console.WriteLine("Format specifiers for integral or floating-point types:")
        Console.WriteLine(msgCurrency    & floatingVal.ToString("C"))
        Console.WriteLine(msgScientific  & floatingVal.ToString("E"))
        Console.WriteLine(msgFixedPoint  & floatingVal.ToString("F"))
        Console.WriteLine(msgGeneral     & floatingVal.ToString("G"))
        Console.WriteLine(msgNumber      & floatingVal.ToString("N"))
        Console.WriteLine(msgPercent     & floatingVal.ToString("P"))
        Console.WriteLine()
        
        ' Display the same values using a CultureInfo object. The CultureInfo class 
        ' implements IFormatProvider.
        Console.WriteLine(msg2)
        
        ' Display the culture used to format the values. 
        ' Create a European culture and change its currency symbol to "euro" because 
        ' this particular code example uses a thread current UI culture that cannot 
        ' display the euro symbol (€).
        ci = New CultureInfo("de-DE")
        ci.NumberFormat.CurrencySymbol = "euro"
        Console.WriteLine("{0,-26}{1}", msgCulture, ci.DisplayName)
        
        ' Display the integral and floating-point values.
        Console.WriteLine("{0,-26}{1}", msgIntegralVal, integralVal)
        Console.WriteLine("{0,-26}{1}", msgFloatingVal, floatingVal)
        Console.WriteLine()
        
        ' Use the format specifiers that are only for integral types.
        Console.WriteLine("Format specifiers only for integral types:")
        Console.WriteLine(msgDecimal     & integralVal.ToString("D", ci))
        Console.WriteLine(msgHexadecimal & integralVal.ToString("X", ci))
        Console.WriteLine()
        
        ' Use the format specifier that is only for the Single and Double 
        ' floating-point types.
        Console.WriteLine("Format specifier only for the Single and Double types:")
        Console.WriteLine(msgRoundTrip   & floatingVal.ToString("R", ci))
        Console.WriteLine()
        
        ' Use the format specifiers that are for integral or floating-point types.
        Console.WriteLine("Format specifiers for integral or floating-point types:")
        Console.WriteLine(msgCurrency    & floatingVal.ToString("C", ci))
        Console.WriteLine(msgScientific  & floatingVal.ToString("E", ci))
        Console.WriteLine(msgFixedPoint  & floatingVal.ToString("F", ci))
        Console.WriteLine(msgGeneral     & floatingVal.ToString("G", ci))
        Console.WriteLine(msgNumber      & floatingVal.ToString("N", ci))
        Console.WriteLine(msgPercent     & floatingVal.ToString("P", ci))
        Console.WriteLine()
    End Sub 'Main
End Class 'Sample
'
'This code example produces the following results:
'
'Standard Numeric Format Specifiers:
'
'Use ToString(String) and the current thread culture.
'
'Culture:                  English (United States)
'Integral value:           -12345
'Floating-point value:     -1234.567
'
'Format specifiers only for integral types:
'(D) Decimal:. . . . . . . -12345
'(X) Hexadecimal:. . . . . FFFFCFC7
'
'Format specifier only for the Single and Double types:
'(R) Round-trip: . . . . . -1234.567
'
'Format specifiers for integral or floating-point types:
'(C) Currency: . . . . . . ($1,234.57)
'(E) Scientific: . . . . . -1.234567E+003
'(F) Fixed point:. . . . . -1234.57
'(G) General (default):. . -1234.567
'(N) Number: . . . . . . . -1,234.57
'(P) Percent:. . . . . . . -123,456.70 %
'
'Use ToString(String, IFormatProvider) and a specified culture.
'
'Culture:                  German (Germany)
'Integral value:           -12345
'Floating-point value:     -1234.567
'
'Format specifiers only for integral types:
'(D) Decimal:. . . . . . . -12345
'(X) Hexadecimal:. . . . . FFFFCFC7
'
'Format specifier only for the Single and Double types:
'(R) Round-trip: . . . . . -1234,567
'
'Format specifiers for integral or floating-point types:
'(C) Currency: . . . . . . -1.234,57 euro
'(E) Scientific: . . . . . -1,234567E+003
'(F) Fixed point:. . . . . -1234,57
'(G) General (default):. . -1234,567
'(N) Number: . . . . . . . -1.234,57
'(P) Percent:. . . . . . . -123.456,70%
'
// This code example demonstrates the ToString(String) and 
// ToString(String, IFormatProvider) methods for integral and
// floating-point numbers, in conjunction with the standard 
// numeric format specifiers.
// This code example uses the System.Int32 integral type and 
// the System.Double floating-point type, but would yield 
// similar results for any of the numeric types. The integral 
// numeric types are System.Byte, SByte, Int16, Int32, Int64, 
// UInt16, UInt32, and UInt64. The floating-point numeric types 
// are Decimal, Single, and Double.

using System;
using System.Globalization;
using System.Threading;

class Sample 
{
    public static void Main() 
    {
// Format a negative integer or floating-point number in various ways.
    int    integralVal = -12345;
    double floatingVal = -1234.567d;

    string msgCurrency =    "(C) Currency: . . . . . . ";
    string msgDecimal  =    "(D) Decimal:. . . . . . . ";
    string msgScientific =  "(E) Scientific: . . . . . ";
    string msgFixedPoint =  "(F) Fixed point:. . . . . ";
    string msgGeneral =     "(G) General (default):. . ";
    string msgNumber =      "(N) Number: . . . . . . . ";
    string msgPercent =     "(P) Percent:. . . . . . . ";
    string msgRoundTrip =   "(R) Round-trip: . . . . . ";
    string msgHexadecimal = "(X) Hexadecimal:. . . . . ";

    string msg1 = "Use ToString(String) and the current thread culture.\n";
    string msg2 = "Use ToString(String, IFormatProvider) and a specified culture.\n";
    string msgCulture     = "Culture:";
    string msgIntegralVal = "Integral value:";
    string msgFloatingVal = "Floating-point value:";

    CultureInfo ci;
//
    Console.Clear();
    Console.WriteLine("Standard Numeric Format Specifiers:\n");
// Display the values.
    Console.WriteLine(msg1);

// Display the thread current culture, which is used to format the values.
    ci = Thread.CurrentThread.CurrentCulture;
    Console.WriteLine("{0,-26}{1}", msgCulture, ci.DisplayName);

// Display the integral and floating-point values.
    Console.WriteLine("{0,-26}{1}", msgIntegralVal, integralVal);
    Console.WriteLine("{0,-26}{1}", msgFloatingVal, floatingVal);
    Console.WriteLine();

// Use the format specifiers that are only for integral types.
    Console.WriteLine("Format specifiers only for integral types:");
    Console.WriteLine(msgDecimal     + integralVal.ToString("D"));
    Console.WriteLine(msgHexadecimal + integralVal.ToString("X"));
    Console.WriteLine();

// Use the format specifier that is only for the Single and Double 
// floating-point types.
    Console.WriteLine("Format specifier only for the Single and Double types:");
    Console.WriteLine(msgRoundTrip   + floatingVal.ToString("R"));
    Console.WriteLine();

// Use the format specifiers that are for integral or floating-point types.
    Console.WriteLine("Format specifiers for integral or floating-point types:");
    Console.WriteLine(msgCurrency    + floatingVal.ToString("C"));
    Console.WriteLine(msgScientific  + floatingVal.ToString("E"));
    Console.WriteLine(msgFixedPoint  + floatingVal.ToString("F"));
    Console.WriteLine(msgGeneral     + floatingVal.ToString("G"));
    Console.WriteLine(msgNumber      + floatingVal.ToString("N"));
    Console.WriteLine(msgPercent     + floatingVal.ToString("P"));
    Console.WriteLine();

// Display the same values using a CultureInfo object. The CultureInfo class 
// implements IFormatProvider.
    Console.WriteLine(msg2);

// Display the culture used to format the values. 
// Create a European culture and change its currency symbol to "euro" because 
// this particular code example uses a thread current UI culture that cannot 
// display the euro symbol (€).
    ci = new CultureInfo("de-DE");
    ci.NumberFormat.CurrencySymbol = "euro";
    Console.WriteLine("{0,-26}{1}", msgCulture, ci.DisplayName);

// Display the integral and floating-point values.
    Console.WriteLine("{0,-26}{1}", msgIntegralVal, integralVal);
    Console.WriteLine("{0,-26}{1}", msgFloatingVal, floatingVal);
    Console.WriteLine();

// Use the format specifiers that are only for integral types.
    Console.WriteLine("Format specifiers only for integral types:");
    Console.WriteLine(msgDecimal     + integralVal.ToString("D", ci));
    Console.WriteLine(msgHexadecimal + integralVal.ToString("X", ci));
    Console.WriteLine();

// Use the format specifier that is only for the Single and Double 
// floating-point types.
    Console.WriteLine("Format specifier only for the Single and Double types:");
    Console.WriteLine(msgRoundTrip   + floatingVal.ToString("R", ci));
    Console.WriteLine();

// Use the format specifiers that are for integral or floating-point types.
    Console.WriteLine("Format specifiers for integral or floating-point types:");
    Console.WriteLine(msgCurrency    + floatingVal.ToString("C", ci));
    Console.WriteLine(msgScientific  + floatingVal.ToString("E", ci));
    Console.WriteLine(msgFixedPoint  + floatingVal.ToString("F", ci));
    Console.WriteLine(msgGeneral     + floatingVal.ToString("G", ci));
    Console.WriteLine(msgNumber      + floatingVal.ToString("N", ci));
    Console.WriteLine(msgPercent     + floatingVal.ToString("P", ci));
    Console.WriteLine();
    }
}
/*
This code example produces the following results:

Standard Numeric Format Specifiers:

Use ToString(String) and the current thread culture.

Culture:                  English (United States)
Integral value:           -12345
Floating-point value:     -1234.567

Format specifiers only for integral types:
(D) Decimal:. . . . . . . -12345
(X) Hexadecimal:. . . . . FFFFCFC7

Format specifier only for the Single and Double types:
(R) Round-trip: . . . . . -1234.567

Format specifiers for integral or floating-point types:
(C) Currency: . . . . . . ($1,234.57)
(E) Scientific: . . . . . -1.234567E+003
(F) Fixed point:. . . . . -1234.57
(G) General (default):. . -1234.567
(N) Number: . . . . . . . -1,234.57
(P) Percent:. . . . . . . -123,456.70 %

Use ToString(String, IFormatProvider) and a specified culture.

Culture:                  German (Germany)
Integral value:           -12345
Floating-point value:     -1234.567

Format specifiers only for integral types:
(D) Decimal:. . . . . . . -12345
(X) Hexadecimal:. . . . . FFFFCFC7

Format specifier only for the Single and Double types:
(R) Round-trip: . . . . . -1234,567

Format specifiers for integral or floating-point types:
(C) Currency: . . . . . . -1.234,57 euro
(E) Scientific: . . . . . -1,234567E+003
(F) Fixed point:. . . . . -1234,57
(G) General (default):. . -1234,567
(N) Number: . . . . . . . -1.234,57
(P) Percent:. . . . . . . -123.456,70%

*/

参照

関連項目

NumberFormatInfo

概念

数値書式指定文字列
標準の数値書式指定文字列の出力例
カスタム数値書式指定文字列

その他の技術情報

型の書式設定