次の方法で共有


Single.ToString メソッド (String, IFormatProvider)

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

Overloads Public Overridable Function ToString( _
   ByVal format As String, _   ByVal provider As IFormatProvider _) As String
[C#]
public virtual string ToString(stringformat,IFormatProviderprovider);
[C++]
public: virtual String* ToString(String* format,IFormatProvider* provider);
[JScript]
public function ToString(
   format : String,provider : IFormatProvider) : String;

パラメータ

  • format
    書式指定。
  • provider
    カルチャ固有の書式情報を提供する IFormatProvider

戻り値

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

解説

戻り値は PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbol または次の書式の文字列のいずれかです。

[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" などがあります。

format が null 参照 (Visual Basic では Nothing) または空の文字列の場合、このインスタンスの戻り値は、一般書式指定子 ("G") を使用して書式設定されます。

既定では、戻り値の有効桁数は 7 桁ですが、内部的には最大 9 桁が保持されています。このインスタンスの値が 7 桁よりも大きい場合、 ToString は、予期される数値ではなく PositiveInfinitySymbol または NegativeInfinitySymbol を返します。より多くの有効桁数が必要な場合は、 format を指定するときに書式指定として "G9" または "R" を使用します。"G9" 書式指定では、常に 9 桁の有効桁数が返されます。"R" 書式指定では、数値が 7 桁で表現できる場合は 7 桁の有効桁数が返され、最大有効桁数でなければ表現できない場合は 9 桁が返されます。

provider パラメータは、カルチャに固有の書式情報を提供する NumberFormatInfo を取得する IFormatProvider です。 provider が null 参照 (Nothing) の場合、戻り値は現在のカルチャの NumberFormatInfo データを使用して書式設定されます。

使用例

[Visual Basic, C#, C++] 書式設定の StringIFormatProvider オブジェクトを使用し、 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