次の方法で共有


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

指定したカルチャに固有の書式による数値の文字列形式を、それと等価な単精度浮動小数点数に変換します。

Overloads Public Shared Function Parse( _
   ByVal s As String, _   ByVal provider As IFormatProvider _) As Single
[C#]
public static float Parse(strings,IFormatProviderprovider);
[C++]
public: static float Parse(String* s,IFormatProvider* provider);
[JScript]
public static function Parse(
   s : String,provider : IFormatProvider) : float;

パラメータ

  • s
    変換する数値を表す文字列。
  • provider
    s に関するカルチャに固有の書式情報を提供する IFormatProvider

戻り値

s で指定した数値または記号に等しい単精度浮動小数点数。

例外

例外の種類 条件
ArgumentNullException s が null 参照 (Visual Basic では Nothing) です。
FormatException s が、有効な書式の数値ではありません。
OverflowException sMinValue 未満の数値か、 MaxValue より大きい数値を表しています。

解説

s パラメータには、 PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbol 、または次の書式の文字列を格納できます。

[ws][sign]integral-digits[.[fractional-digits]][e[sign]exponential-digits][ws]

省略可能な項目は、角かっこ ([および]) で囲まれています。"digits" という語を含む項目は、0 から 9 までの一連の数字から構成されます。

  • ws
    一連の空白文字。
  • sign
    負の記号または正の記号。
  • integral-digits
    数値の整数部分を指定する一連の数字。一連の整数の桁は、区切り記号によって区切られる場合があります。たとえば、カルチャによっては、桁区切り文字としてコンマ (,) が使用されます。小数の桁が存在する場合は、整数の桁はなくてもかまいません。
  • '.'
    カルチャに固有の小数点記号。
  • fractional-digits
    数値の小数部分を指定する一連の数字。
  • 'e'
    指数表記を示す大文字または小文字の 'e'。
  • exponential-digits
    指数部を指定する一連の数字。

s の例には、"100"、"-123,456,789"、"123.45e+6"、"+500"、"5e2"、"3.1416"、"600."、"-.123"、"-Infinity" などがあります。

Parse のこのバージョンでは、 NumberStyles 値、 Float および AllowThousands 、および指定した数値書式情報を使用します。 provider が null 参照 (Visual Basic では Nothing) の場合や、 NumberFormatInfo を取得できない場合は、現在のシステムのカルチャの書式情報が使用されます。

数値書式の詳細については、「 書式設定の概要 」のトピックを参照してください。

使用例

[Visual Basic, C#, C++] IFormatProvider オブジェクトを使用し、 String 形式の Single の値を Parse メソッドで解析するコード例を次に示します。

 
' Example of the Single.Parse( ) methods.
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic

Module SingleParseDemo
    
    ' Get the exception type name; remove the namespace prefix.
    Function GetExceptionType( ex As Exception ) As String

        Dim exceptionType   As String = ex.GetType( ).ToString( )
        Return exceptionType.Substring( _
            exceptionType.LastIndexOf( "."c ) + 1 )
    End Function

    Sub SingleParse( styles As NumberStyles, _
        provider As IFormatProvider )

        Dim singleFormats As String( ) = { _
            " 987.654E-2",    " 987,654E-2",    "(98765,43210)", _
            "9,876,543.210",  "9.876.543,210",  "98_76_54_32,19" }
            
        ' Parse each string in the singleFormats array, using 
        ' NumberStyles and IFormatProvider, if specified.
        Dim singleString As String
        For Each singleString In  singleFormats

            Dim singleNumber As Single

            ' Display the first part of the output line.
            Console.Write( "  Parse of {0,-20}", _
                String.Format( """{0}""", singleString ) )

            ' Use the appropriate Single.Parse overload, based on 
            ' the parameters that are specified.
            Try
                If provider Is Nothing Then
                    If styles < 0 Then
                        singleNumber = Single.Parse( singleString )
                    Else
                        singleNumber = _
                            Single.Parse( singleString, styles )
                    End If
                ElseIf styles < 0 Then
                    singleNumber = _
                        Single.Parse( singleString, provider )
                Else
                    singleNumber = Single.Parse( _
                        singleString, styles, provider )
                End If
                
                ' Display the resulting value if Parse succeeded.
                Console.WriteLine( "success: {0}", singleNumber )

            ' Display the exception type if Parse failed.
            Catch ex As Exception
                Console.WriteLine( "failed:  {0}", _
                    GetExceptionType( ex ) )
            End Try
        Next singleString
    End Sub 
    
    Sub Main( )
        Console.WriteLine( "This example of" & vbCrLf & _
            "  Single.Parse( String )," & vbCrLf & _
            "  Single.Parse( String, NumberStyles )," & vbCrLf & _
            "  Single.Parse( String, IFormatProvider ), and" & _
            vbCrLf & "  Single.Parse( String, NumberStyles, " & _
            "IFormatProvider )" & vbCrLf & "generates the " & _
            "following output when run in the [{0}] culture.", _
            CultureInfo.CurrentCulture.Name )
        Console.WriteLine( "Several string representations " & _
            "of Single values are parsed." )

        ' Do not use IFormatProvider or NumberStyles.
        Console.WriteLine( vbCrLf & _
            "NumberStyles and IFormatProvider are not " & _
            "used; current culture is [{0}]:", _
            CultureInfo.CurrentCulture.Name )
        SingleParse( CType( -1, NumberStyles ), Nothing )

        ' Use the NumberStyle for Currency.
        Console.WriteLine( vbCrLf & "NumberStyles.Currency " & _
            "is used; IFormatProvider is not used:" )
        SingleParse( NumberStyles.Currency, Nothing )
            
        ' 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 )
            
        Console.WriteLine( vbCrLf & _
            "NumberStyles is not used; [{0}] culture " & _
            "IFormatProvider is used:", culture.Name )
        SingleParse( CType( -1, NumberStyles ), 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 the NumberFormatInfo object as the IFormatProvider.
        Console.WriteLine( vbCrLf & _
            "NumberStyles.Currency is used, group size = 2, " & _
            "separator = ""_"":" )
        SingleParse( NumberStyles.Currency, numInfo )
    End Sub 
End Module 

' This example of
'   Single.Parse( String ),
'   Single.Parse( String, NumberStyles ),
'   Single.Parse( String, IFormatProvider ), and
'   Single.Parse( String, NumberStyles, IFormatProvider )
' generates the following output when run in the [en-US] culture.
' Several string representations of Single values are parsed.
' 
' NumberStyles and IFormatProvider are not used; current culture is [en-US]:
'   Parse of " 987.654E-2"       success: 9.87654
'   Parse of " 987,654E-2"       success: 9876.54
'   Parse of "(98765,43210)"     failed:  FormatException
'   Parse of "9,876,543.210"     success: 9876543
'   Parse of "9.876.543,210"     failed:  FormatException
'   Parse of "98_76_54_32,19"    failed:  FormatException
' 
' NumberStyles.Currency is used; IFormatProvider is not used:
'   Parse of " 987.654E-2"       failed:  FormatException
'   Parse of " 987,654E-2"       failed:  FormatException
'   Parse of "(98765,43210)"     success: -9.876543E+09
'   Parse of "9,876,543.210"     success: 9876543
'   Parse of "9.876.543,210"     failed:  FormatException
'   Parse of "98_76_54_32,19"    failed:  FormatException
' 
' NumberStyles is not used; [nl-NL] culture IFormatProvider is used:
'   Parse of " 987.654E-2"       success: 9876.54
'   Parse of " 987,654E-2"       success: 9.87654
'   Parse of "(98765,43210)"     failed:  FormatException
'   Parse of "9,876,543.210"     failed:  FormatException
'   Parse of "9.876.543,210"     success: 9876543
'   Parse of "98_76_54_32,19"    failed:  FormatException
' 
' NumberStyles.Currency is used, group size = 2, separator = "_":
'   Parse of " 987.654E-2"       failed:  FormatException
'   Parse of " 987,654E-2"       failed:  FormatException
'   Parse of "(98765,43210)"     success: -98765.43
'   Parse of "9,876,543.210"     failed:  FormatException
'   Parse of "9.876.543,210"     success: 9876543
'   Parse of "98_76_54_32,19"    success: 9.876543E+07

[C#] 
// Example of the Single.Parse( ) methods.
using System;
using System.Globalization;

class SingleParseDemo
{
    // Get the exception type name; remove the namespace prefix.
    static string GetExceptionType( Exception ex )
    {
        string exceptionType = ex.GetType( ).ToString( );
        return exceptionType.Substring( 
            exceptionType.LastIndexOf( '.' )+1 );
    }

    // Parse each string in the singleFormats array, using 
    // NumberStyles and IFormatProvider, if specified.
    static void SingleParse( NumberStyles styles, 
        IFormatProvider provider )
    {
        string[ ] singleFormats = {
            " 987.654E-2",   " 987,654E-2",    "(98765,43210)", 
            "9,876,543.210", "9.876.543,210",  "98_76_54_32,19" };
            
        foreach( string singleString in singleFormats )
        {
            float singleNumber;

            // Display the first part of the output line.
            Console.Write( "  Parse of {0,-20}", 
                String.Format( "\"{0}\"", singleString ) );

            try
            {
                // Use the appropriate Single.Parse overload, based 
                // on the parameters that are specified.
                if( provider == null )
                {
                    if( styles < 0 )
                        singleNumber = Single.Parse( singleString );
                    else
                        singleNumber = 
                            Single.Parse( singleString, styles );
                }
                else if( styles < 0 )
                    singleNumber = 
                        Single.Parse( singleString, provider );
                else
                    singleNumber = 
                        Single.Parse( singleString, styles, provider );
                
                // Display the resulting value if Parse succeeded.
                Console.WriteLine( "success: {0}", singleNumber );
            }
            catch( Exception ex )
            {
                // Display the exception type if Parse failed.
                Console.WriteLine( "failed:  {0}", 
                    GetExceptionType( ex ) );
            }
        }
    }
    
    public static void Main( )
    {
        Console.WriteLine( "This example of\n" +
            "  Single.Parse( String ),\n" +
            "  Single.Parse( String, NumberStyles ),\n" +
            "  Single.Parse( String, IFormatProvider ), and\n" +
            "  Single.Parse( String, NumberStyles, " +
            "IFormatProvider )\ngenerates the " +
            "following output when run in the [{0}] culture.", 
            CultureInfo.CurrentCulture.Name );
        Console.WriteLine( "Several string representations " +
            "of Single values are parsed." );

        // Do not use IFormatProvider or NumberStyles.
        Console.WriteLine( "\nNumberStyles and IFormatProvider " +
            "are not used; current culture is [{0}]:", 
            CultureInfo.CurrentCulture.Name );
        SingleParse( ( (NumberStyles)( -1 ) ), null );

        // Use the NumberStyle for Currency.
        Console.WriteLine( "\nNumberStyles.Currency " +
            "is used; IFormatProvider is not used:" );
        SingleParse( NumberStyles.Currency, null );
            
        // 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 );
            
        Console.WriteLine( "\nNumberStyles is not used; " +
            "[{0}] culture IFormatProvider is used:", 
            culture.Name );
        SingleParse( ( (NumberStyles)( -1 ) ), 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 the NumberFormatInfo object as the IFormatProvider.
        Console.WriteLine( "\nNumberStyles.Currency is used, " +
            "group size = 2, separator = \"_\":" );
        SingleParse( NumberStyles.Currency, numInfo );
    }
}

/*
This example of
  Single.Parse( String ),
  Single.Parse( String, NumberStyles ),
  Single.Parse( String, IFormatProvider ), and
  Single.Parse( String, NumberStyles, IFormatProvider )
generates the following output when run in the [en-US] culture.
Several string representations of Single values are parsed.

NumberStyles and IFormatProvider are not used; current culture is [en-US]:
  Parse of " 987.654E-2"       success: 9.87654
  Parse of " 987,654E-2"       success: 9876.54
  Parse of "(98765,43210)"     failed:  FormatException
  Parse of "9,876,543.210"     success: 9876543
  Parse of "9.876.543,210"     failed:  FormatException
  Parse of "98_76_54_32,19"    failed:  FormatException

NumberStyles.Currency is used; IFormatProvider is not used:
  Parse of " 987.654E-2"       failed:  FormatException
  Parse of " 987,654E-2"       failed:  FormatException
  Parse of "(98765,43210)"     success: -9.876543E+09
  Parse of "9,876,543.210"     success: 9876543
  Parse of "9.876.543,210"     failed:  FormatException
  Parse of "98_76_54_32,19"    failed:  FormatException

NumberStyles is not used; [nl-NL] culture IFormatProvider is used:
  Parse of " 987.654E-2"       success: 9876.54
  Parse of " 987,654E-2"       success: 9.87654
  Parse of "(98765,43210)"     failed:  FormatException
  Parse of "9,876,543.210"     failed:  FormatException
  Parse of "9.876.543,210"     success: 9876543
  Parse of "98_76_54_32,19"    failed:  FormatException

NumberStyles.Currency is used, group size = 2, separator = "_":
  Parse of " 987.654E-2"       failed:  FormatException
  Parse of " 987,654E-2"       failed:  FormatException
  Parse of "(98765,43210)"     success: -98765.43
  Parse of "9,876,543.210"     failed:  FormatException
  Parse of "9.876.543,210"     success: 9876543
  Parse of "98_76_54_32,19"    success: 9.876543E+07
*/ 

[C++] 
// Example of the Single::Parse( ) methods.
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;
using namespace System::Collections;

// Get the exception type name; remove the namespace prefix.
String* GetExceptionType( Exception* ex )
{
    String* exceptionType = ex->GetType( )->ToString( );
    return exceptionType->Substring( 
        exceptionType->LastIndexOf( '.' ) + 1 );
}

// Parse each string in the singleFormats array, using 
// NumberStyles and IFormatProvider, if specified.
void SingleParse( NumberStyles styles, IFormatProvider* provider )
{
    String* singleFormats[ ] = __gc new String*[ 6 ];

        singleFormats[ 0 ] = S" 987.654E-2";
        singleFormats[ 1 ] = S"987,654E-2 ";
        singleFormats[ 2 ] = S"(98765,43210)";
        singleFormats[ 3 ] = S"9,876,543.210";
        singleFormats[ 4 ] = S"9.876.543,210";
        singleFormats[ 5 ] = S"98_76_54_32,19";

    // Code foreach( String* singleString in singleFormats ).
    IEnumerator*    myEnum = singleFormats->GetEnumerator( );
    while( myEnum->MoveNext( ) )
    {
        String* singleString = 
            __try_cast<String*>( myEnum->Current );
        float singleNumber;

        // Display the first part of the output line.
        Console::Write( S"  Parse of {0,-20}", 
            String::Format( S"\"{0}\"", singleString ) );

        try
        {
            // Use the appropriate Single::Parse overload, based on 
            // the parameters that are specified.
            if( provider == (IFormatProvider*)0 )
            {
                if( styles < 0 )
                    singleNumber = Single::Parse( singleString );
                else
                    singleNumber = 
                        Single::Parse( singleString, styles );
            }
            else if( styles < 0 )
                singleNumber = 
                    Single::Parse( singleString, provider );
            else
                singleNumber = 
                    Single::Parse( singleString, styles, provider );
            
            // Display the resulting value if Parse succeeded.
            Console::WriteLine( S"success: {0}", 
                __box( singleNumber ) );
        }
        catch( Exception* ex )
        {
            // Display the exception type if Parse failed.
            Console::WriteLine( S"failed:  {0}", 
                GetExceptionType( ex ) );
        }
    }
}

void main( )
{
    IFormatProvider* null = (IFormatProvider*)0;

    Console::WriteLine( S"This example of\n" 
        S"  Single::Parse( String* ),\n" 
        S"  Single::Parse( String*, NumberStyles ),\n" 
        S"  Single::Parse( String*, IFormatProvider* ), and\n" 
        S"  Single::Parse( String*, NumberStyles, " 
        S"IFormatProvider* )\ngenerates the " 
        S"following output when run in the [{0}] culture.", 
        CultureInfo::CurrentCulture->Name );
    Console::WriteLine( S"Several string representations " 
        S"of Single values are parsed." );

    // Do not use IFormatProvider or NumberStyles.
    Console::WriteLine( S"\nNumberStyles and IFormatProvider " 
        S"are not used; current culture is [{0}]:", 
        CultureInfo::CurrentCulture->Name );
    SingleParse( ( (NumberStyles)( -1 ) ), null );

    // Use the NumberStyle for Currency.
    Console::WriteLine( S"\nNumberStyles::Currency " 
        S"is used; IFormatProvider is not used:" );
    SingleParse( NumberStyles::Currency, null );
        
    // 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 );
        
    Console::WriteLine( S"\nNumberStyles is not used; " 
        S"[{0}] culture IFormatProvider is used:", 
        culture->Name );
    SingleParse( ( (NumberStyles)( -1 ) ), 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 the NumberFormatInfo object as the IFormatProvider.
    Console::WriteLine( S"\nNumberStyles::Currency is used, " 
        S"group size = 2, separator = \"_\":" );
    SingleParse( NumberStyles::Currency, numInfo );
}

/*
This example of
  Single::Parse( String* ),
  Single::Parse( String*, NumberStyles ),
  Single::Parse( String*, IFormatProvider* ), and
  Single::Parse( String*, NumberStyles, IFormatProvider* )
generates the following output when run in the [en-US] culture.
Several string representations of Single values are parsed.

NumberStyles and IFormatProvider are not used; current culture is [en-US]:
  Parse of " 987.654E-2"       success: 9.87654
  Parse of "987,654E-2 "       success: 9876.54
  Parse of "(98765,43210)"     failed:  FormatException
  Parse of "9,876,543.210"     success: 9876543
  Parse of "9.876.543,210"     failed:  FormatException
  Parse of "98_76_54_32,19"    failed:  FormatException

NumberStyles::Currency is used; IFormatProvider is not used:
  Parse of " 987.654E-2"       failed:  FormatException
  Parse of "987,654E-2 "       failed:  FormatException
  Parse of "(98765,43210)"     success: -9.876543E+09
  Parse of "9,876,543.210"     success: 9876543
  Parse of "9.876.543,210"     failed:  FormatException
  Parse of "98_76_54_32,19"    failed:  FormatException

NumberStyles is not used; [nl-NL] culture IFormatProvider is used:
  Parse of " 987.654E-2"       success: 9876.54
  Parse of "987,654E-2 "       success: 9.87654
  Parse of "(98765,43210)"     failed:  FormatException
  Parse of "9,876,543.210"     failed:  FormatException
  Parse of "9.876.543,210"     success: 9876543
  Parse of "98_76_54_32,19"    failed:  FormatException

NumberStyles::Currency is used, group size = 2, separator = "_":
  Parse of " 987.654E-2"       failed:  FormatException
  Parse of "987,654E-2 "       failed:  FormatException
  Parse of "(98765,43210)"     success: -98765.43
  Parse of "9,876,543.210"     failed:  FormatException
  Parse of "9.876.543,210"     success: 9876543
  Parse of "98_76_54_32,19"    success: 9.876543E+07
*/ 

[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.Parse オーバーロードの一覧 | 書式設定の概要 | ToString