Single.ToString メソッド (IFormatProvider)
指定したカルチャに固有の書式情報を使用して、このインスタンスの数値を、それと等価な文字列形式に変換します。
Overloads Public Overridable Function ToString( _
ByVal provider As IFormatProvider _) As String
[C#]
public virtual string ToString(IFormatProviderprovider);
[C++]
public: virtual String* ToString(IFormatProvider* provider);
[JScript]
public function ToString(
provider : IFormatProvider) : String;
パラメータ
- provider
カルチャ固有の書式情報を提供する IFormatProvider 。
戻り値
provider で指定された、このインスタンスの値の文字列形式。
解説
戻り値は PositiveInfinitySymbol 、 NegativeInfinitySymbol 、 NaNSymbol または次の書式の文字列のいずれかです。
[sign]integral-digits[.[fractional-digits]][e[sign]exponential-digits]
省略可能な項目は、角かっこ ([および]) で囲まれています。"digits" という語を含む項目は、0 から 9 までの一連の数字から構成されます。
- sign
負の記号または正の記号。 - integral-digits
数値の整数部分を指定する一連の数字。小数の桁が存在する場合は、整数の桁はなくてもかまいません。 - '.'
カルチャに固有の小数点記号。 - fractional-digits
数値の小数部分を指定する一連の数字。 - 'e'
指数表記を示す小文字の 'e'。 - exponential-digits
指数部を指定する一連の数字。
戻り値の例には、"100"、"-123,456,789"、"123.45e+6"、"500"、"3.1416"、"600"、"-0.123"、"-Infinity" などがあります。
このインスタンスは一般書式指定子 ("G") で書式設定されます。
provider パラメータは、カルチャに固有の書式情報を提供する NumberFormatInfo を取得する IFormatProvider です。 provider が null 参照 (Visual Basic では Nothing) の場合、戻り値は現在のカルチャの NumberFormatInfo データを使用して書式設定されます。
使用例
[Visual Basic, C#, C++] IFormatProvider オブジェクトを使用し、 ToString メソッドで Single の値を String に変換するコード例を次に示します。
' Example for the Single.ToString( ) methods.
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic
Module SingleToStringDemo
Sub Main( )
Dim singleValue As Single = 11876.54321
Dim format As String = " {0,-30}{1}"
Console.WriteLine( "This example of" & vbCrLf & _
" Single.ToString( ), " & vbCrLf & _
" Single.ToString( String )," & vbCrLf & _
" Single.ToString( IFormatProvider ), and " & vbCrLf & _
" Single.ToString( String, IFormatProvider )" & _
vbCrLf & "generates the following output when run in " & _
"the [{0}] culture. " & vbCrLf & "A Single number is " & _
"formatted with various combinations of format " & _
"" & vbCrLf & "strings and IFormatProvider.", _
CultureInfo.CurrentCulture.Name )
' Format the number without and with format strings.
Console.WriteLine( vbCrLf & "IFormatProvider is not " & _
"used; the default culture is [{0}]:", _
CultureInfo.CurrentCulture.Name )
Console.WriteLine( format, _
"No format string:", singleValue.ToString( ) )
Console.WriteLine( format, _
"'N5' format string:", singleValue.ToString( "N5" ) )
Console.WriteLine( format, _
"'E' format string:", singleValue.ToString( "E" ) )
Console.WriteLine( format, _
"'E5' format string:", singleValue.ToString( "E5" ) )
' Create a CultureInfo object for another culture. Use
' [Dutch - The Netherlands] unless the current culture
' is Dutch language. In that case use [English - U.S.].
Dim cultureName As String = IIf( _
CultureInfo.CurrentCulture.Name.Substring( 0, 2 ) = _
"nl", "en-US", "nl-NL" )
Dim culture As New CultureInfo( cultureName )
' Use the CultureInfo object for an IFormatProvider.
Console.WriteLine( vbCrLf & "A CultureInfo object " & _
"for [{0}] is used for the IFormatProvider: ", _
cultureName )
Console.WriteLine( format, "No format string:", _
singleValue.ToString( culture ) )
Console.WriteLine( format, "'N5' format string:", _
singleValue.ToString( "N5", culture ) )
Console.WriteLine( format, "'E' format string:", _
singleValue.ToString( "E", culture ) )
' Get the NumberFormatInfo object from CultureInfo, and
' then change the digit group size to 2 and the digit
' separator to '_'.
Dim numInfo As NumberFormatInfo = culture.NumberFormat
numInfo.NumberGroupSizes = New Integer( ) { 2 }
numInfo.NumberGroupSeparator = "_"
' Use a NumberFormatInfo object for IFormatProvider.
Console.WriteLine( vbCrLf & _
"A NumberFormatInfo object with digit group " & _
"size = 2 and " & vbCrLf & "digit separator " & _
"= '_' is used for the IFormatProvider:" )
Console.WriteLine( format, "'N' format string:", _
singleValue.ToString( "N", culture ) )
Console.WriteLine( format, "'E' format string:", _
singleValue.ToString( "E", culture ) )
End Sub
End Module
' This example of
' Single.ToString( ),
' Single.ToString( String ),
' Single.ToString( IFormatProvider ), and
' Single.ToString( String, IFormatProvider )
' generates the following output when run in the [en-US] culture.
' A Single number is formatted with various combinations of format
' strings and IFormatProvider.
'
' IFormatProvider is not used; the default culture is [en-US]:
' No format string: 11876.54
' 'N5' format string: 11,876.54000
' 'E' format string: 1.187654E+004
' 'E5' format string: 1.18765E+004
'
' A CultureInfo object for [nl-NL] is used for the IFormatProvider:
' No format string: 11876,54
' 'N5' format string: 11.876,54000
' 'E' format string: 1,187654E+004
'
' A NumberFormatInfo object with digit group size = 2 and
' digit separator = '_' is used for the IFormatProvider:
' 'N' format string: 1_18_76,54
' 'E' format string: 1,187654E+004
[C#]
// Example for the Single.ToString( ) methods.
using System;
using System.Globalization;
class SingleToStringDemo
{
public static void Main( )
{
float singleValue = 11876.54321F;
string format = " {0,-30}{1}";
Console.WriteLine( "This example of\n" +
" Single.ToString( ), \n" +
" Single.ToString( String ),\n" +
" Single.ToString( IFormatProvider ), and \n" +
" Single.ToString( String, IFormatProvider )\n" +
"generates the following output when run in the [{0}] " +
"culture. \nA Single number is formatted with various " +
"combinations of format \nstrings and IFormatProvider.",
CultureInfo.CurrentCulture.Name );
// Format the number without and with format strings.
Console.WriteLine( "\nIFormatProvider is not " +
"used; the default culture is [{0}]:",
CultureInfo.CurrentCulture.Name );
Console.WriteLine( format, "No format string:",
singleValue.ToString( ) );
Console.WriteLine( format, "'N5' format string:",
singleValue.ToString( "N5" ) );
Console.WriteLine( format, "'E' format string:",
singleValue.ToString( "E" ) );
Console.WriteLine( format, "'E5' format string:",
singleValue.ToString( "E5" ) );
// Create a CultureInfo object for another culture. Use
// [Dutch - The Netherlands] unless the current culture
// is Dutch language. In that case use [English - U.S.].
string cultureName =
CultureInfo.CurrentCulture.Name.Substring( 0, 2 ) == "nl" ?
"en-US" : "nl-NL";
CultureInfo culture = new CultureInfo( cultureName );
// Use the CultureInfo object for an IFormatProvider.
Console.WriteLine( "\nA CultureInfo object " +
"for [{0}] is used for the IFormatProvider: ",
cultureName );
Console.WriteLine( format, "No format string:",
singleValue.ToString( culture ) );
Console.WriteLine( format, "'N5' format string:",
singleValue.ToString( "N5", culture ) );
Console.WriteLine( format, "'E' format string:",
singleValue.ToString( "E", culture ) );
// Get the NumberFormatInfo object from CultureInfo, and
// then change the digit group size to 2 and the digit
// separator to '_'.
NumberFormatInfo numInfo = culture.NumberFormat;
numInfo.NumberGroupSizes = new int[ ]{ 2 };
numInfo.NumberGroupSeparator = "_";
// Use a NumberFormatInfo object for IFormatProvider.
Console.WriteLine( "\nA NumberFormatInfo object with digit " +
"group size = 2 and \ndigit separator = '_' is used " +
"for the IFormatProvider:" );
Console.WriteLine( format, "'N' format string:",
singleValue.ToString( "N", culture ) );
Console.WriteLine( format, "'E' format string:",
singleValue.ToString( "E", culture ) );
}
}
/*
This example of
Single.ToString( ),
Single.ToString( String ),
Single.ToString( IFormatProvider ), and
Single.ToString( String, IFormatProvider )
generates the following output when run in the [en-US] culture.
A Single number is formatted with various combinations of format
strings and IFormatProvider.
IFormatProvider is not used; the default culture is [en-US]:
No format string: 11876.54
'N5' format string: 11,876.54000
'E' format string: 1.187654E+004
'E5' format string: 1.18765E+004
A CultureInfo object for [nl-NL] is used for the IFormatProvider:
No format string: 11876,54
'N5' format string: 11.876,54000
'E' format string: 1,187654E+004
A NumberFormatInfo object with digit group size = 2 and
digit separator = '_' is used for the IFormatProvider:
'N' format string: 1_18_76,54
'E' format string: 1,187654E+004
*/
[C++]
// Example for the Single::ToString( ) methods.
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;
void main( )
{
float singleValue = 11876.54321F;
String* format = S" {0,-30}{1}";
Console::WriteLine( S"This example of\n"
S" Single::ToString( ), \n"
S" Single::ToString( String* ),\n"
S" Single::ToString( IFormatProvider* ), and \n"
S" Single::ToString( String*, IFormatProvider* )\n"
S"generates the following output when run in the [{0}] "
S"culture. \nA Single number is formatted with various "
S"combinations of format \nstrings and IFormatProvider.",
CultureInfo::CurrentCulture->Name );
// Format the number without and with format strings.
Console::WriteLine( S"\nIFormatProvider is not "
S"used; the default culture is [{0}]:",
CultureInfo::CurrentCulture->Name );
Console::WriteLine( format, S"No format string:",
singleValue.ToString( ) );
Console::WriteLine( format, S"'N5' format string:",
singleValue.ToString( S"N5" ) );
Console::WriteLine( format, S"'E' format string:",
singleValue.ToString( S"E" ) );
Console::WriteLine( format, S"'E5' format string:",
singleValue.ToString( S"E5" ) );
// Create a CultureInfo object for another culture. Use
// [Dutch - The Netherlands] unless the current culture
// is Dutch language. In that case use [English - U.S.].
String* cultureName =
CultureInfo::CurrentCulture->Name->Substring( 0, 2 ) == S"nl" ?
S"en-US" : S"nl-NL";
CultureInfo* culture = new CultureInfo( cultureName );
// Use the CultureInfo object for an IFormatProvider.
Console::WriteLine( S"\nA CultureInfo object "
S"for [{0}] is used for the IFormatProvider: ",
cultureName );
Console::WriteLine( format, S"No format string:",
singleValue.ToString( culture ) );
Console::WriteLine( format, S"'N5' format string:",
singleValue.ToString( S"N5", culture ) );
Console::WriteLine( format, S"'E' format string:",
singleValue.ToString( S"E", culture ) );
// Get the NumberFormatInfo object from CultureInfo, and
// then change the digit group size to 2 and the digit
// separator to '_'.
NumberFormatInfo* numInfo = culture->NumberFormat;
Int32 sizes __gc [] = { 2 };
numInfo->NumberGroupSizes = sizes;
numInfo->NumberGroupSeparator = S"_";
// Use a NumberFormatInfo object for IFormatProvider.
Console::WriteLine( S"\nA NumberFormatInfo object with digit "
S"group size = 2 and \ndigit separator = '_' is used "
S"for the IFormatProvider:" );
Console::WriteLine( format, S"'N' format string:",
singleValue.ToString( S"N", culture ) );
Console::WriteLine( format, S"'E' format string:",
singleValue.ToString( S"E", culture ) );
}
/*
This example of
Single::ToString( ),
Single::ToString( String* ),
Single::ToString( IFormatProvider* ), and
Single::ToString( String*, IFormatProvider* )
generates the following output when run in the [en-US] culture.
A Single number is formatted with various combinations of format
strings and IFormatProvider.
IFormatProvider is not used; the default culture is [en-US]:
No format string: 11876.54
'N5' format string: 11,876.54000
'E' format string: 1.187654E+004
'E5' format string: 1.18765E+004
A CultureInfo object for [nl-NL] is used for the IFormatProvider:
No format string: 11876,54
'N5' format string: 11.876,54000
'E' format string: 1,187654E+004
A NumberFormatInfo object with digit group size = 2 and
digit separator = '_' is used for the IFormatProvider:
'N' format string: 1_18_76,54
'E' format string: 1,187654E+004
*/
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard
参照
Single 構造体 | Single メンバ | System 名前空間 | Single.ToString オーバーロードの一覧 | 書式設定の概要 | Parse | String