数値の文字列形式を、それと等価な単精度浮動小数点数に変換します。
オーバーロードの一覧
数値の文字列形式を、それと等価な単精度浮動小数点数に変換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function Parse(String) As Single
指定したカルチャに固有の書式による数値の文字列形式を、それと等価な単精度浮動小数点数に変換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function Parse(String, IFormatProvider) As Single
[C++] public: static float Parse(String*, IFormatProvider*);
[JScript] public static function Parse(String, IFormatProvider) : float;
指定したスタイルでの数値の文字列形式を、それと等価な単精度浮動小数点数に変換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function Parse(String, NumberStyles) As Single
[JScript] public static function Parse(String, NumberStyles) : float;
指定したスタイルおよびカルチャに固有の書式による数値の文字列形式を、それと等価な単精度浮動小数点数に変換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Shared Function Parse(String, NumberStyles, IFormatProvider) As Single
[C#] public static float Parse(string, NumberStyles, IFormatProvider);
[C++] public: static float Parse(String*, NumberStyles, IFormatProvider*);
[JScript] public static function Parse(String, NumberStyles, IFormatProvider) : float;
使用例
[Visual Basic, C#, C++] NumberStyles の値と IFormatProvider オブジェクトを使用し、 String 形式の Single の値を Parse メソッドで解析するコード例を次に示します。
[Visual Basic, C#, C++] メモ ここでは、Parse のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。
' 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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。