Double.ToString メソッド
定義
このインスタンスの数値を、それと等価な文字列形式に変換します。Converts the numeric value of this instance to its equivalent string representation.
オーバーロード
ToString(String, IFormatProvider) |
このインスタンスの数値を、指定した書式およびカルチャ固有の書式情報を使用して、それと等価な文字列形式に変換します。Converts the numeric value of this instance to its equivalent string representation using the specified format and culture-specific format information. |
ToString(String) |
指定した書式を使用して、このインスタンスの数値を、それと等価な文字列形式に変換します。Converts the numeric value of this instance to its equivalent string representation, using the specified format. |
ToString(IFormatProvider) |
このインスタンスの数値を、指定したカルチャ固有の書式情報を使用して、それと等価な文字列形式に変換します。Converts the numeric value of this instance to its equivalent string representation using the specified culture-specific format information. |
ToString() |
このインスタンスの数値を、それと等価な文字列形式に変換します。Converts the numeric value of this instance to its equivalent string representation. |
ToString(String, IFormatProvider)
このインスタンスの数値を、指定した書式およびカルチャ固有の書式情報を使用して、それと等価な文字列形式に変換します。Converts the numeric value of this instance to its equivalent string representation using the specified format and culture-specific format information.
public:
virtual System::String ^ ToString(System::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
数値書式指定文字列。A numeric format string.
- provider
- IFormatProvider
カルチャ固有の書式情報を提供するオブジェクト。An object that supplies culture-specific formatting information.
戻り値
format
および provider
で指定された、このインスタンスの値の文字列形式。The string representation of the value of this instance as specified by format
and provider
.
実装
例
次の例では、複数の異なるカルチャに対してサポートされている各標準数値書式指定子を使用して、@no__t 0 の値を表示します。The following example displays a Double value using each of the supported standard numeric format specifiers for several different cultures.
using namespace System;
using namespace System::Globalization;
int main(array<System::String ^> ^args)
{
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 %
return 0;
}
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 %
Dim value As Double = 16325.62901
Dim specifier As String
Dim culture As CultureInfo
' 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 と IFormatProvider を取得して ToString を使用する方法を示します。The following example illustrates the use of ToString, taking a String and an IFormatProvider as parameters.
public ref class Temperature: public IFormattable
{
// IFormattable.ToString implementation.
public:
virtual String^ ToString( String^ format, IFormatProvider^ provider )
{
if ( format != nullptr )
{
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 );
}
protected:
// The value holder
double m_value;
public:
property double Value
{
double get()
{
return m_value;
}
void set( double value )
{
m_value = value;
}
}
property double Celsius
{
double get()
{
return (m_value - 32.0) / 1.8;
}
void set( double value )
{
m_value = 1.8 * value + 32.0;
}
}
};
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;
}
}
}
Public Class Temperature
Implements IFormattable
Public Overloads Function ToString(ByVal format As String, ByVal provider As IFormatProvider) As String _
Implements IFormattable.ToString
If Not (format Is Nothing) Then
If format.Equals("F") Then
Return [String].Format("{0}'F", Me.Value.ToString())
End If
If format.Equals("C") Then
Return [String].Format("{0}'C", Me.Celsius.ToString())
End If
End If
Return m_value.ToString(format, provider)
End Function
' The value holder
Protected m_value As Double
Public Property Value() As Double
Get
Return m_value
End Get
Set(ByVal Value As Double)
m_value = Value
End Set
End Property
Public Property Celsius() As Double
Get
Return (m_value - 32) / 1.8
End Get
Set(ByVal Value As Double)
m_value = Value * 1.8 + 32
End Set
End Property
End Class
注釈
@No__t-0 メソッドは、指定したカルチャの指定した書式で Double の値を書式設定します。The ToString(String, IFormatProvider) method formats a Double value in a specified format of a specified culture. 別の形式またはカルチャを指定する場合は、次のように ToString メソッドの他のオーバーロードを使用します。If you want to specify a different format or culture, use the other overloads of the ToString method, as follows:
形式を使用するにはTo use format | カルチャの場合For culture | オーバーロードを使用するUse the overload |
---|---|---|
既定 ("G") 形式Default ("G") format | 既定の (現在の) カルチャDefault (current) culture | ToString() |
既定 ("G") 形式Default ("G") format | 特定のカルチャA specific culture | ToString(IFormatProvider) |
特定の形式または有効桁数A specific format or precision | 既定の (現在の) カルチャDefault (current) culture | ToString(String) |
戻り値には、format
で指定されているように、0、NegativeInfinitySymbol、NaNSymbol、または数値の文字列形式を @no__t できます。The return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or the string representation of a number, as specified by format
.
@No__t-0 パラメーターには、任意の有効な標準数値書式指定子を指定できます。ただし、D と X を除き、カスタム数値書式指定子の任意の組み合わせを使用できます。The format
parameter can be any valid standard numeric format specifier except for D and X, as well as any combination of custom numeric format specifiers. @No__t-0 @no__t が-1 または空の文字列の場合、このインスタンスの戻り値は、一般的な数値書式指定子 ("G") で書式設定されます。If format
is null
or an empty string, the return value for this instance is formatted with the general numeric format specifier ("G").
.NET Framework では、さまざまな書式設定がサポートされています。詳細については、次の書式設定に関するトピックを参照してください。The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:
数値書式指定子の詳細については、「標準の数値書式指定文字列」および「カスタム数値書式指定文字列」を参照してください。For more information about numeric format specifiers, see Standard Numeric Format Strings and Custom Numeric Format Strings.
書式設定の詳細については、「型の書式設定」を参照してください。For more information about formatting, see Formatting Types.
@No__t-0 パラメーターは、GetFormat メソッドが @no__t 3 のオブジェクトを返す @no__t 1 の実装です。The provider
parameter is an IFormatProvider implementation whose GetFormat method returns a NumberFormatInfo object. 通常、provider
は、@no__t 1 オブジェクトまたは @no__t 2 オブジェクトです。Typically, provider
is a CultureInfo object or a NumberFormatInfo object. @No__t-0 パラメーターは、書式設定で使用されるカルチャ固有の情報を提供します。The provider
parameter supplies culture-specific information used in formatting. @No__t-0 が null
の場合、戻り値は現在のカルチャの NumberFormatInfo オブジェクトを使用して書式設定されます。If provider
is null
, the return value is formatted using the NumberFormatInfo object for the current culture.
既定では、戻り値の精度は15桁のみですが、内部的には最大17桁が保持されます。By default, the return value only contains 15 digits of precision although a maximum of 17 digits is maintained internally. このインスタンスの値が15桁を超える場合、ToString は、予期された数ではなく PositiveInfinitySymbol または NegativeInfinitySymbol を返します。If the value of this instance has greater than 15 digits, ToString returns PositiveInfinitySymbol or NegativeInfinitySymbol instead of the expected number. 精度をさらに高める必要がある場合は、"G17" 書式指定を使用して format
を指定します。この形式では、常に17桁の有効桁数が返されます。 "R" は、数字を有効桁数または17桁の数字で表すことができる場合に15桁を返します。最大有効桁数。If you require more precision, specify format
with the "G17" format specification, which always returns 17 digits of precision, or "R", which returns 15 digits if the number can be represented with that precision or 17 digits if the number can only be represented with maximum precision.
注意 (呼び出し元)
場合によっては、/platform:x64
スイッチまたは /platform:anycpu
スイッチを使用してコンパイルして 64 ビット システムで実行すると、"R" 標準の数値書式指定文字列で書式設定される Double 値のラウンドトリップに失敗することがあります。In some cases, Double values formatted with the "R" standard numeric format string do not successfully round-trip if compiled using the /platform:x64
or /platform:anycpu
switches and run on 64-bit systems. この問題を回避するには、"G17" 標準の数値書式指定文字列を使用して Double 値を書式設定することができます。To work around this problem, you can format Double values by using the "G17" standard numeric format string. 次の例では、ラウンドトリップに失敗する Double 値を持つ "R" 書式指定文字列を使用しています。元の値のラウンドトリップに成功する "G17" 書式指定文字列も使用しています。The following example uses the "R" format string with a Double value that does not round-trip successfully, and also uses the "G17" format string to successfully round-trip the original value.
[!code-csharpSystem.Double.ToString#5] [!code-vbSystem.Double.ToString#5][!code-csharpSystem.Double.ToString#5] [!code-vbSystem.Double.ToString#5]
こちらもご覧ください
- Parse(String)
- .NET での型の書式設定Formatting Types in .NET
- 標準の数値書式指定文字列Standard Numeric Format Strings
- カスタム数値書式指定文字列Custom Numeric Format Strings
- 方法: 数値に先行するゼロを埋め込むHow to: Pad a Number with Leading Zeros
- サンプル: .NET Core WinForms 書式指定ユーティリティC#()Sample: .NET Core WinForms Formatting Utility (C#)
- サンプル: .NET Core WinForms の書式設定ユーティリティ (Visual Basic)Sample: .NET Core WinForms Formatting Utility (Visual Basic)
ToString(String)
指定した書式を使用して、このインスタンスの数値を、それと等価な文字列形式に変換します。Converts the numeric value of this instance to its equivalent string representation, using the specified format.
public:
System::String ^ ToString(System::String ^ format);
public string ToString (string format);
override this.ToString : string -> string
Public Function ToString (format As String) As String
パラメーター
- format
- String
数値書式指定文字列。A numeric format string.
戻り値
format
で指定された、このインスタンスの値の文字列形式。The string representation of the value of this instance as specified by format
.
例外
format
が無効です。format
is invalid.
例
次の例では、数値を定義し、"C" 標準の数値書式指定文字列を使用して通貨値として書式設定し、"N" 標準の数値書式指定文字列を使用して小数点以下3桁の数値として書式指定します。The following example defines a numeric value and formats it as a currency value by using the "C" standard numeric format string and as a numeric value to three decimal places by using the "N" standard numeric format string. 結果の文字列は、en-us カルチャの規則を使用して書式設定されます。The result strings are formatted by using the conventions of the en-US culture. 数値書式指定文字列の詳細については、「標準の数値書式指定文字列」および「カスタム数値書式指定文字列」を参照してください。For more information on numeric format strings, see Standard Numeric Format Strings and Custom Numeric Format Strings.
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
Module Example
Public Sub Main()
Dim number As Double = 1764.3789
' Format as a currency value.
Console.WriteLine(number.ToString("C"))
' Format as a numeric value with 3 decimal places.
Console.WriteLine(number.ToString("N3"))
End Sub
End Module
' The example displays the following output:
' $1,764.38
' 1,764.379
次の例では、サポートされている標準数値書式指定子と3つのカスタム数値書式指定文字列を使用して、複数の @no__t 0 値を表示します。The following example displays several Double values using the supported standard numeric format specifiers together with three custom numeric format strings. これらのカスタム書式指定文字列の1つは、@no__t 0 の値に先行ゼロを埋め込む方法を示しています。One of those custom format strings illustrates how to pad a Single value with leading zeros. また、この例では、"R" を除き、各標準書式指定子で精度指定子を使用します。In addition, the example uses precision specifiers with each standard format specifier except for "R". 精度指定子の値の範囲は 0 ~ 3 です。The values of the precision specifiers range from 0 to 3. 数値を文字列に変換するために、この例では en-us カルチャの書式指定規則を使用します。To convert the numeric values to strings, the example uses the formatting conventions of the en-US culture.
using namespace System;
void main()
{
array<Double>^ numbers= {1054.32179, -195489100.8377, 1.0437E21,
-1.0573e-05};
array<String^>^ specifiers = { "C", "E", "e", "F", "G", "N", "P",
"R", "#,000.000", "0.###E-000",
"000,000,000,000.00###" };
for each (Double number in numbers)
{
Console::WriteLine("Formatting of {0}:", number);
for each (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:
// 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
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
Dim numbers() As Double = {1054.32179, -195489100.8377, 1.0437E21, _
-1.0573e-05}
Dim specifiers() As String = { "C", "E", "e", "F", "G", "N", "P", _
"R", "#,000.000", "0.###E-000", _
"000,000,000,000.00###" }
For Each number As Double In numbers
Console.WriteLine("Formatting of {0}:", number)
For Each specifier As String In specifiers
Console.WriteLine(" {0,-22} {1}",
specifier + ":", number.ToString(specifier))
' Add precision specifiers from 0 to 3.
If specifier.Length = 1 And Not specifier.Equals("R") Then
For precision As Integer = 0 To 3
Dim pSpecifier As String = String.Format("{0}{1}", specifier, precision)
Console.WriteLine(" {0,-22} {1}",
pSpecifier + ":", number.ToString(pSpecifier))
Next
Console.WriteLine()
End If
Next
Console.WriteLine()
Next
' The example displays the following output:
' 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
注釈
@No__t-0 メソッドは、現在のカルチャの規則を使用して、指定した書式で Double の値を書式設定します。The ToString(String) method formats a Double value in a specified format by using the conventions of the current culture. 別の形式またはカルチャを指定する場合は、次のように ToString メソッドの他のオーバーロードを使用します。If you want to specify a different format or culture, use the other overloads of the ToString method, as follows:
形式を使用するにはTo use format | カルチャの場合For culture | オーバーロードを使用するUse the overload |
---|---|---|
既定 ("G") 形式Default ("G") format | 既定の (現在の) カルチャDefault (current) culture | ToString() |
既定 ("G") 形式Default ("G") format | 特定のカルチャA specific culture | ToString(IFormatProvider) |
特定の形式または有効桁数A specific format or precision | 特定のカルチャA specific culture | ToString(String, IFormatProvider) |
戻り値には、format
で指定されているように、0、NegativeInfinitySymbol、NaNSymbol、または数値の文字列形式を @no__t できます。The return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or the string representation of a number, as specified by format
.
@No__t-0 パラメーターには、任意の有効な標準数値書式指定子を指定できます。ただし、D と X を除き、カスタム数値書式指定子の任意の組み合わせを使用できます。The format
parameter can be any valid standard numeric format specifier except for D and X, as well as any combination of custom numeric format specifiers. @No__t-0 @no__t が-1 または空の文字列の場合、戻り値は一般的な数値書式指定子 ("G") で書式設定されます。If format
is null
or an empty string, the return value is formatted with the general numeric format specifier ("G").
.NET Framework では、さまざまな書式設定がサポートされています。詳細については、次の書式設定に関するトピックを参照してください。The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:
数値書式指定子の詳細については、「標準の数値書式指定文字列」および「カスタム数値書式指定文字列」を参照してください。For more information about numeric format specifiers, see Standard Numeric Format Strings and Custom Numeric Format Strings.
書式設定の詳細については、「型の書式設定」を参照してください。For more information about formatting, see Formatting Types.
既定では、戻り値の精度は15桁のみですが、内部的には最大17桁が保持されます。By default, the return value only contains 15 digits of precision although a maximum of 17 digits is maintained internally. このインスタンスの値が15桁を超える場合、ToString は、予期された数ではなく PositiveInfinitySymbol または NegativeInfinitySymbol を返します。If the value of this instance has greater than 15 digits, ToString returns PositiveInfinitySymbol or NegativeInfinitySymbol instead of the expected number. 精度をさらに高める必要がある場合は、"G17" 書式指定を使用して format
を指定します。この形式では、常に17桁の有効桁数が返されます。 "R" は、数字を有効桁数または17桁の数字で表すことができる場合に15桁を返します。最大有効桁数。If you require more precision, specify format
with the "G17" format specification, which always returns 17 digits of precision, or "R", which returns 15 digits if the number can be represented with that precision or 17 digits if the number can only be represented with maximum precision.
注意 (呼び出し元)
場合によっては、/platform:x64
スイッチまたは /platform:anycpu
スイッチを使用してコンパイルして 64 ビット システムで実行すると、"R" 標準の数値書式指定文字列で書式設定される Double 値のラウンドトリップに失敗することがあります。In some cases, Double values formatted with the "R" standard numeric format string do not successfully round-trip if compiled using the /platform:x64
or /platform:anycpu
switches and run on 64-bit systems. この問題を回避するには、"G17" 標準の数値書式指定文字列を使用して Double 値を書式設定することができます。To work around this problem, you can format Double values by using the "G17" standard numeric format string. 次の例では、ラウンドトリップに失敗する Double 値を持つ "R" 書式指定文字列を使用しています。元の値のラウンドトリップに成功する "G17" 書式指定文字列も使用しています。The following example uses the "R" format string with a Double value that does not round-trip successfully, and also uses the "G17" format string to successfully round-trip the original value.
[!code-csharpSystem.Double.ToString#6] [!code-vbSystem.Double.ToString#6][!code-csharpSystem.Double.ToString#6] [!code-vbSystem.Double.ToString#6]
こちらもご覧ください
ToString(IFormatProvider)
このインスタンスの数値を、指定したカルチャ固有の書式情報を使用して、それと等価な文字列形式に変換します。Converts the numeric value of this instance to its equivalent string representation using the specified culture-specific format information.
public:
System::String ^ ToString(IFormatProvider ^ provider);
public string ToString (IFormatProvider provider);
override this.ToString : IFormatProvider -> string
Public Function ToString (provider As IFormatProvider) As String
パラメーター
- provider
- IFormatProvider
カルチャ固有の書式情報を提供するオブジェクト。An object that supplies culture-specific formatting information.
戻り値
provider
で指定された、このインスタンスの値の文字列形式。The string representation of the value of this instance as specified by provider
.
実装
例
次の例では、複数の異なるカルチャを表す CultureInfo オブジェクトを使用して、2つの @no__t 0 値の文字列形式を表示します。The following example displays the string representation of two Double values using CultureInfo objects that represent several different cultures.
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
Dim value As Double
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 と IFormatProvider を取得して ToString を使用する方法を示します。The following example illustrates the use of ToString, taking a String and an IFormatProvider as parameters.
public ref class Temperature: public IFormattable
{
// IFormattable.ToString implementation.
public:
virtual String^ ToString( String^ format, IFormatProvider^ provider )
{
if ( format != nullptr )
{
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 );
}
protected:
// The value holder
double m_value;
public:
property double Value
{
double get()
{
return m_value;
}
void set( double value )
{
m_value = value;
}
}
property double Celsius
{
double get()
{
return (m_value - 32.0) / 1.8;
}
void set( double value )
{
m_value = 1.8 * value + 32.0;
}
}
};
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;
}
}
}
Public Class Temperature
Implements IFormattable
Public Overloads Function ToString(ByVal format As String, ByVal provider As IFormatProvider) As String _
Implements IFormattable.ToString
If Not (format Is Nothing) Then
If format.Equals("F") Then
Return [String].Format("{0}'F", Me.Value.ToString())
End If
If format.Equals("C") Then
Return [String].Format("{0}'C", Me.Celsius.ToString())
End If
End If
Return m_value.ToString(format, provider)
End Function
' The value holder
Protected m_value As Double
Public Property Value() As Double
Get
Return m_value
End Get
Set(ByVal Value As Double)
m_value = Value
End Set
End Property
Public Property Celsius() As Double
Get
Return (m_value - 32) / 1.8
End Get
Set(ByVal Value As Double)
m_value = Value * 1.8 + 32
End Set
End Property
End Class
注釈
@No__t-0 メソッドは、指定されたカルチャの既定 ("G" または一般) 形式で Double の値を書式設定します。The ToString(IFormatProvider) method formats a Double value in the default ("G", or general) format of a specified culture. 別の形式またはカルチャを指定する場合は、次のように ToString メソッドの他のオーバーロードを使用します。If you want to specify a different format or culture, use the other overloads of the ToString method, as follows:
形式を使用するにはTo use format | カルチャの場合For culture | オーバーロードを使用するUse the overload |
---|---|---|
既定 ("G") 形式Default ("G") format | 既定 (現在)Default (current) | ToString() |
特定の形式または有効桁数A specific format or precision | 既定の (現在の) カルチャDefault (current) culture | ToString(String) |
特定の形式または有効桁数A specific format or precision | 特定のカルチャA specific culture | ToString(String, IFormatProvider) |
戻り値には @no__t 0、NegativeInfinitySymbol、NaNSymbol、または形式の文字列を指定できます。The return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or a string of the form:
[sign] 整数桁 [. [小数点以下の桁数]] [e [sign] 指数数字][sign]integral-digits[.[fractional-digits]][e[sign]exponential-digits]
省略可能な要素は、角かっこ ([および]) で囲まれています。Optional elements are framed in square brackets ([ and ]). "Digits" という用語を含む要素は、0から9までの一連の数字で構成されます。Elements that contain the term "digits" consist of a series of numeric characters ranging from 0 to 9. 次の表に示す要素がサポートされています。The elements listed in the following table are supported.
要素Element | 説明Description |
---|---|
signsign | 負の符号または正の記号。A negative sign or positive sign symbol. |
integral-digitsintegral-digits | 数値の整数部を指定する一連の数字。A series of digits specifying the integral part of the number. 小数部がある場合は、整数の数字を省略できます。Integral-digits can be absent if there are fractional-digits. |
'.''.' | カルチャ固有の小数点の記号。A culture-specific decimal point symbol. |
fractional-digitsfractional-digits | 数値の小数部を指定する一連の数字。A series of digits specifying the fractional part of the number. |
'e''e' | 指数 (科学的) 表記を示す小文字の "e"。A lowercase character 'e', indicating exponential (scientific) notation. |
exponential-digitsexponential-digits | 指数を指定する一連の数字。A series of digits specifying an exponent. |
戻り値の例としては、"100"、"-123456789"、"123.45 e + 6"、"500"、"3.1416"、"600"、"-0.123"、"-無限大" などがあります。Some examples of the return value are "100", "-123,456,789", "123.45e+6", "500", "3.1416", "600", "-0.123", and "-Infinity".
このインスタンスは、一般的な数値書式指定子 ("G") で書式設定されます。This instance is formatted with the general numeric format specifier ("G").
.NET Framework では、さまざまな書式設定がサポートされています。詳細については、次の書式設定に関するトピックを参照してください。The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:
数値書式指定子の詳細については、「標準の数値書式指定文字列」および「カスタム数値書式指定文字列」を参照してください。For more information about numeric format specifiers, see Standard Numeric Format Strings and Custom Numeric Format Strings.
書式設定の詳細については、「型の書式設定」を参照してください。For more information about formatting, see Formatting Types.
@No__t-0 パラメーターは、GetFormat メソッドが @no__t 3 のオブジェクトを返す @no__t 1 の実装です。The provider
parameter is an IFormatProvider implementation whose GetFormat method returns a NumberFormatInfo object. 通常、provider
は、@no__t 1 オブジェクトまたは @no__t 2 オブジェクトです。Typically, provider
is a CultureInfo object or a NumberFormatInfo object. @No__t-0 パラメーターは、書式設定で使用されるカルチャ固有の情報を提供します。The provider
parameter supplies culture-specific information used in formatting. @No__t-0 が null
の場合、戻り値は現在のカルチャの NumberFormatInfo オブジェクトを使用して書式設定されます。If provider
is null
, the return value is formatted using the NumberFormatInfo object for the current culture.
こちらもご覧ください
ToString()
このインスタンスの数値を、それと等価な文字列形式に変換します。Converts the numeric value of this instance to its equivalent string representation.
public:
override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String
戻り値
このインスタンスの値の文字列形式。The string representation of the value of this instance.
例
次の例では、既定の Double.ToString() メソッドを使用して、複数の Double 値の文字列形式を表示します。The following example uses the default Double.ToString() method to display the string representations of a number of Double values.
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());
Dim number As Double
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の使用方法を示しています。The following example illustrates the use of ToString.
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 );
done = true;
}
catch ( FormatException^ )
{
Console::WriteLine( "You did not enter a number." );
}
catch ( Exception^ e )
{
Console::WriteLine( "An exception occurred while parsing your response: {0}", e );
}
}
while ( !done );
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);
Dim Done As Boolean = False
Dim Inp As String
Do
Console.Write("Enter a real number: ")
inp = Console.ReadLine()
Try
D = Double.Parse(inp)
Console.WriteLine("You entered " + D.ToString() + ".")
Done = True
Catch e As FormatException
Console.WriteLine("You did not enter a number.")
Catch e As ArgumentNullException
Console.WriteLine("You did not supply any input.")
Catch e As OverflowException
Console.WriteLine("The value you entered, {0}, is out of range.", inp)
End Try
Loop While Not Done
注釈
@No__t-0 メソッドは、現在のカルチャの既定 ("G" または一般) 形式で Double の値を書式設定します。The ToString() method formats a Double value in the default ("G", or general) format of the current culture. 異なる形式、有効桁数、またはカルチャを指定する場合は、次のように ToString メソッドの他のオーバーロードを使用します。If you want to specify a different format, precision, or culture, use the other overloads of the ToString method, as follows:
形式を使用するにはTo use format | カルチャの場合For culture | オーバーロードを使用するUse the overload |
---|---|---|
既定 ("G") 形式Default ("G") format | 特定のカルチャA specific culture | ToString(IFormatProvider) |
特定の形式または有効桁数A specific format or precision | 既定の (現在の) カルチャDefault (current) culture | ToString(String) |
特定の形式または有効桁数A specific format or precision | 特定のカルチャA specific culture | ToString(String, IFormatProvider) |
戻り値には @no__t 0、NegativeInfinitySymbol、NaNSymbol、または形式の文字列を指定できます。The return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or a string of the form:
[sign] 整数桁 [. [小数点以下の桁数]] [e [sign] 指数数字][sign]integral-digits[.[fractional-digits]][e[sign]exponential-digits]
省略可能な要素は、角かっこ ([および]) で囲まれています。Optional elements are framed in square brackets ([ and ]). "Digits" という用語を含む要素は、0から9までの一連の数字で構成されます。Elements that contain the term "digits" consist of a series of numeric characters ranging from 0 to 9. 次の表に示す要素がサポートされています。The elements listed in the following table are supported.
要素Element | 説明Description |
---|---|
signsign | 負の符号または正の記号。A negative sign or positive sign symbol. |
integral-digitsintegral-digits | 数値の整数部を指定する一連の数字。A series of digits specifying the integral part of the number. 小数部がある場合は、整数の数字を省略できます。Integral-digits can be absent if there are fractional-digits. |
'.''.' | カルチャ固有の小数点の記号。A culture-specific decimal point symbol. |
fractional-digitsfractional-digits | 数値の小数部を指定する一連の数字。A series of digits specifying the fractional part of the number. |
'e''e' | 指数 (科学的) 表記を示す小文字の "e"。A lowercase character 'e', indicating exponential (scientific) notation. |
exponential-digitsexponential-digits | 指数を指定する一連の数字。A series of digits specifying an exponent. |
戻り値の例としては、"100"、"-123456789"、"123.45 e + 6"、"500"、"3.1416"、"600"、"-0.123"、"-無限大" などがあります。Some examples of the return value are "100", "-123,456,789", "123.45e+6", "500", "3.1416", "600", "-0.123", and "-Infinity".
.NET Framework では、さまざまな書式設定がサポートされています。詳細については、次の書式設定に関するトピックを参照してください。The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:
数値書式指定子の詳細については、「標準の数値書式指定文字列」および「カスタム数値書式指定文字列」を参照してください。For more information about numeric format specifiers, see Standard Numeric Format Strings and Custom Numeric Format Strings.
書式設定の詳細については、「型の書式設定」を参照してください。For more information about formatting, see Formatting Types.