次の方法で共有


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

指定したカルチャ固有の書式情報を使用して、 String 形式の数値をそれと等価の Decimal に変換します。

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

パラメータ

  • s
    変換する数値を格納している String
  • provider
    s に関するカルチャに固有の書式情報を提供する IFormatProvider

戻り値

provider で指定されたとおりの、 s に格納されている数値と等価の Decimal

例外

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

解説

パラメータ s が、次の形式の数値を格納しています。

[ws][sign]digits[.fractional-digits][ws]

角かっこ ('[' および ']') で囲まれている項目は省略可能です。その他の項目は次のとおりです。

  • ws
    省略可能な空白。
  • sign
    省略可能な符号。
  • digits
    0 から 9 までの一連の数字。
  • '.'
    カルチャに固有の小数点記号。
  • fractional-digits
    0 から 9 までの一連の数字。

パラメータ s は、 NumberStyles.Number スタイルを使用して解釈されます。

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

Decimal の精度は 29 桁です。 s が表す数値が 29 桁以上であるが、小数部を含んでおり、 MaxValueMinValue の範囲内に含まれる場合、その数値は切り捨てられるのではなく、29 桁の近似値に丸められます。

使用例

[Visual Basic, C#, C++] Parse メソッドの複数のオーバーロードを使用して String 形式の Decimal 値を解析するコード例を次に示します。

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

Module DecimalParseDemo
    
    Sub DecimalParse( styles As NumberStyles, _
        provider As IFormatProvider )

        Dim decimalFormats As String( ) = { _
            "9876543210.9876543210",  "9876543210,9876543210", _
            "(9876543210,9876543210)", _
            "9,876,543,210,987,654.3210", _
            "9.876.543.210.987.654,3210", _
            "98_7654_3210_9876,543210" }
            
        ' Parse each string in the decimalFormats array, using 
        ' NumberStyles and IFormatProvider, if specified.
        Dim decimalString As String
        For Each decimalString In  decimalFormats

            Dim decimalNumber As Decimal

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

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

            ' Display the exception message if Parse failed.
            ' Truncate the message so it fits on the output line.
            Catch ex As Exception
                Console.WriteLine( "failed: {0}", _
                    ex.Message.Substring( 0, 29 ) )
            End Try
        Next decimalString
    End Sub 
    
    Sub Main( )
        Console.WriteLine( "This example of" & vbCrLf & _
            "  Decimal.Parse( String )," & vbCrLf & _
            "  Decimal.Parse( String, NumberStyles )," & vbCrLf & _
            "  Decimal.Parse( String, IFormatProvider ), and" & _
            vbCrLf & "  Decimal.Parse( String, NumberStyles, " & _
            "IFormatProvider )" & vbCrLf & "generates the " & _
            "following output when run in the [{0}] culture.", _
            CultureInfo.CurrentCulture.Name )
        Console.WriteLine( "Several string representations " & _
            "of Decimal values are parsed." )

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

        ' Use the NumberStyle for Currency.
        Console.WriteLine( vbCrLf & "NumberStyles.Currency " & _
            "is used; IFormatProvider is not used:" )
        DecimalParse( 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 )
        DecimalParse( CType( -1, NumberStyles ), culture )
            
        ' Get the NumberFormatInfo object from CultureInfo, and
        ' then change the digit group size to 4 and the digit
        ' separator to '_'.
        Dim numInfo As NumberFormatInfo = culture.NumberFormat
        numInfo.NumberGroupSizes = New Integer( ) { 4 }
        numInfo.NumberGroupSeparator = "_"
            
        ' Use the NumberFormatInfo object as the IFormatProvider.
        Console.WriteLine( vbCrLf & _
            "NumberStyles.Currency is used, group size = 4, " & _
            "separator = ""_"":" )
        DecimalParse( NumberStyles.Currency, numInfo )
    End Sub 
End Module 

' This example of
'   Decimal.Parse( String ),
'   Decimal.Parse( String, NumberStyles ),
'   Decimal.Parse( String, IFormatProvider ), and
'   Decimal.Parse( String, NumberStyles, IFormatProvider )
' generates the following output when run in the [en-US] culture.
' Several string representations of Decimal values are parsed.
' 
' NumberStyles and IFormatProvider are not used; current culture is [en-US]:
'   Parse of "9876543210.9876543210"      succeeded: 9876543210.9876543210
'   Parse of "9876543210,9876543210"      succeeded: 98765432109876543210
'   Parse of "(9876543210,9876543210)"    failed: Input string was not in a cor
'   Parse of "9,876,543,210,987,654.3210" succeeded: 9876543210987654.3210
'   Parse of "9.876.543.210.987.654,3210" failed: Input string was not in a cor
'   Parse of "98_7654_3210_9876,543210"   failed: Input string was not in a cor
' 
' NumberStyles.Currency is used; IFormatProvider is not used:
'   Parse of "9876543210.9876543210"      succeeded: 9876543210.9876543210
'   Parse of "9876543210,9876543210"      succeeded: 98765432109876543210
'   Parse of "(9876543210,9876543210)"    succeeded: -98765432109876543210
'   Parse of "9,876,543,210,987,654.3210" succeeded: 9876543210987654.3210
'   Parse of "9.876.543.210.987.654,3210" failed: Input string was not in a cor
'   Parse of "98_7654_3210_9876,543210"   failed: Input string was not in a cor
' 
' NumberStyles is not used; [nl-NL] culture IFormatProvider is used:
'   Parse of "9876543210.9876543210"      succeeded: 98765432109876543210
'   Parse of "9876543210,9876543210"      succeeded: 9876543210.9876543210
'   Parse of "(9876543210,9876543210)"    failed: Input string was not in a cor
'   Parse of "9,876,543,210,987,654.3210" failed: Input string was not in a cor
'   Parse of "9.876.543.210.987.654,3210" succeeded: 9876543210987654.3210
'   Parse of "98_7654_3210_9876,543210"   failed: Input string was not in a cor
' 
' NumberStyles.Currency is used, group size = 4, separator = "_":
'   Parse of "9876543210.9876543210"      succeeded: 98765432109876543210
'   Parse of "9876543210,9876543210"      succeeded: 9876543210.9876543210
'   Parse of "(9876543210,9876543210)"    succeeded: -9876543210.9876543210
'   Parse of "9,876,543,210,987,654.3210" failed: Input string was not in a cor
'   Parse of "9.876.543.210.987.654,3210" succeeded: 9876543210987654.3210
'   Parse of "98_7654_3210_9876,543210"   succeeded: 98765432109876.543210

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

class DecimalParseDemo
{
    static void DecimalParse( NumberStyles styles, 
        IFormatProvider provider )
    {
        string[ ] decimalFormats = { "9876543210.9876543210", 
            "9876543210,9876543210", "(9876543210,9876543210)", 
            "9,876,543,210,987,654.3210", 
            "9.876.543.210.987.654,3210", 
            "98_7654_3210_9876,543210" };
            
        // Parse each string in the decimalFormats array, using 
        // NumberStyles and IFormatProvider, if specified.
        foreach ( string decimalString in decimalFormats )
        {
            decimal decimalNumber;

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

            try
            {
                // Use the appropriate Decimal.Parse overload, 
                // based on the parameters that are specified.
                if( provider == null )
                    if ( styles < 0 )
                        decimalNumber = 
                            Decimal.Parse( decimalString );
                    else
                        decimalNumber = Decimal.Parse( 
                            decimalString, styles );
                else 
                    if( styles < 0 )
                        decimalNumber = Decimal.Parse(
                            decimalString, provider );
                    else
                        decimalNumber = Decimal.Parse(
                            decimalString, styles, provider );
                
                // Display the resulting value if Parse succeeded.
                Console.WriteLine( "succeeded: {0}", 
                    decimalNumber );

            }
            catch( Exception ex )
            {
                // Display the exception message if Parse failed.
                // Truncate it to fit on the output line.
                Console.WriteLine( "failed: {0}", 
                    ex.Message.Substring( 0, 31 ) );
            }
        }
    }
    
    static void Main( )
    {
        Console.WriteLine( "This example of\n  Decimal.Parse( " +
            "String ),\n  Decimal.Parse( String, NumberStyles " +
            "),\n  Decimal.Parse( String, IFormatProvider ), " +
            "and\n  Decimal.Parse( String, NumberStyles, " +
            "IFormatProvider )\ngenerates the " +
            "following output when run in the [{0}] culture.", 
            CultureInfo.CurrentCulture.Name );
        Console.WriteLine( "Several string representations " +
            "of Decimal values are parsed." );

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

        // Use the NumberStyle for Currency.
        Console.WriteLine( "\nNumberStyles.Currency " +
            "is used; IFormatProvider is not used:" );
        DecimalParse( 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 );
        DecimalParse( (NumberStyles)(-1), culture );
            
        // Get the NumberFormatInfo object from CultureInfo, and
        // then change the digit group size to 4 and the digit
        // separator to '_'.
        NumberFormatInfo numInfo = culture.NumberFormat;
        numInfo.NumberGroupSizes = new int[ ] { 4 };
        numInfo.NumberGroupSeparator = "_";
            
        // Use the NumberFormatInfo object as the IFormatProvider.
        Console.WriteLine( "\nNumberStyles.Currency is used, " +
            "group size = 4, separator = \"_\":" );
        DecimalParse( NumberStyles.Currency, numInfo );
    } 
} 

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

NumberStyles and IFormatProvider are not used; current culture is [en-US]:
  Parse of "9876543210.9876543210"      succeeded: 9876543210.9876543210
  Parse of "9876543210,9876543210"      succeeded: 98765432109876543210
  Parse of "(9876543210,9876543210)"    failed: Input string was not in a corre
  Parse of "9,876,543,210,987,654.3210" succeeded: 9876543210987654.3210
  Parse of "9.876.543.210.987.654,3210" failed: Input string was not in a corre
  Parse of "98_7654_3210_9876,543210"   failed: Input string was not in a corre

NumberStyles.Currency is used; IFormatProvider is not used:
  Parse of "9876543210.9876543210"      succeeded: 9876543210.9876543210
  Parse of "9876543210,9876543210"      succeeded: 98765432109876543210
  Parse of "(9876543210,9876543210)"    succeeded: -98765432109876543210
  Parse of "9,876,543,210,987,654.3210" succeeded: 9876543210987654.3210
  Parse of "9.876.543.210.987.654,3210" failed: Input string was not in a corre
  Parse of "98_7654_3210_9876,543210"   failed: Input string was not in a corre

NumberStyles is not used; [nl-NL] culture IFormatProvider is used:
  Parse of "9876543210.9876543210"      succeeded: 98765432109876543210
  Parse of "9876543210,9876543210"      succeeded: 9876543210.9876543210
  Parse of "(9876543210,9876543210)"    failed: Input string was not in a corre
  Parse of "9,876,543,210,987,654.3210" failed: Input string was not in a corre
  Parse of "9.876.543.210.987.654,3210" succeeded: 9876543210987654.3210
  Parse of "98_7654_3210_9876,543210"   failed: Input string was not in a corre

NumberStyles.Currency is used, group size = 4, separator = "_":
  Parse of "9876543210.9876543210"      succeeded: 98765432109876543210
  Parse of "9876543210,9876543210"      succeeded: 9876543210.9876543210
  Parse of "(9876543210,9876543210)"    succeeded: -9876543210.9876543210
  Parse of "9,876,543,210,987,654.3210" failed: Input string was not in a corre
  Parse of "9.876.543.210.987.654,3210" succeeded: 9876543210987654.3210
  Parse of "98_7654_3210_9876,543210"   succeeded: 98765432109876.543210
*/

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

#define null (IFormatProvider*)0

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

        decimalFormats[ 0 ] = S"9876543210.9876543210";
        decimalFormats[ 1 ] = S"9876543210,9876543210";
        decimalFormats[ 2 ] = S"(9876543210,9876543210)";
        decimalFormats[ 3 ] = S"9,876,543,210,987,654.3210";
        decimalFormats[ 4 ] = S"9.876.543.210.987.654,3210";
        decimalFormats[ 5 ] = S"98_7654_3210_9876,543210";

    // Code foreach( String* decimalString in decimalFormats ).
    IEnumerator*    myEnum = decimalFormats->GetEnumerator( );
    while( myEnum->MoveNext( ) )
    {
        String* decimalString = 
            __try_cast<String*>( myEnum->Current );
        Decimal decimalNumber;

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

        try
        {
            // Use the appropriate Decimal::Parse overload, based 
            // on the parameters that are specified.
            if( provider == null )
                if ( styles < 0 )
                    decimalNumber = 
                        Decimal::Parse( decimalString );
                else
                    decimalNumber = Decimal::Parse( 
                        decimalString, styles );
            else 
                if( styles < 0 )
                    decimalNumber = Decimal::Parse(
                        decimalString, provider );
                else
                    decimalNumber = Decimal::Parse(
                        decimalString, styles, provider );
            
            // Display the resulting value if Parse succeeded.
            Console::WriteLine( S"succeeded: {0}", 
                __box( decimalNumber ) );

        }
        catch( Exception* ex )
        {
            // Display the exception message if Parse failed.
            // Truncate it to fit on the output line.
            Console::WriteLine( S"failed: {0}", 
                ex->Message->Substring( 0, 31 ) );
        }
    }
} 

void main( )
{
    Console::WriteLine( S"This example of\n"
        S"  Decimal::Parse( String* ),\n"
        S"  Decimal::Parse( String*, NumberStyles ),\n"
        S"  Decimal::Parse( String*, IFormatProvider* ), and\n"
        S"  Decimal::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 Decimal values are parsed." );

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

    // Use the NumberStyle for Currency.
    Console::WriteLine( S"\nNumberStyles::Currency " 
        S"is used; IFormatProvider is not used:" );
    DecimalParse( 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; [{0}] " 
        S"culture IFormatProvider is used:", culture->Name );
    DecimalParse( (NumberStyles)(-1), culture );
        
    // Get the NumberFormatInfo object from CultureInfo, and
    // then change the digit group size to 4 and the digit
    // separator to '_'.
    NumberFormatInfo* numInfo = culture->NumberFormat;
    Int32 sizes __gc [] = { 4 };
    numInfo->NumberGroupSizes = sizes;
    numInfo->NumberGroupSeparator = S"_";
        
    // Use the NumberFormatInfo object as the IFormatProvider.
    Console::WriteLine( S"\nNumberStyles::Currency is used, " 
        S"group size = 4, separator = \"_\":" );
    DecimalParse( NumberStyles::Currency, numInfo );
}

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

NumberStyles and IFormatProvider are not used; current culture is [en-US]:
  Parse of "9876543210.9876543210"      succeeded: 9876543210.9876543210
  Parse of "9876543210,9876543210"      succeeded: 98765432109876543210
  Parse of "(9876543210,9876543210)"    failed: Input string was not in a corre
  Parse of "9,876,543,210,987,654.3210" succeeded: 9876543210987654.3210
  Parse of "9.876.543.210.987.654,3210" failed: Input string was not in a corre
  Parse of "98_7654_3210_9876,543210"   failed: Input string was not in a corre

NumberStyles::Currency is used; IFormatProvider is not used:
  Parse of "9876543210.9876543210"      succeeded: 9876543210.9876543210
  Parse of "9876543210,9876543210"      succeeded: 98765432109876543210
  Parse of "(9876543210,9876543210)"    succeeded: -98765432109876543210
  Parse of "9,876,543,210,987,654.3210" succeeded: 9876543210987654.3210
  Parse of "9.876.543.210.987.654,3210" failed: Input string was not in a corre
  Parse of "98_7654_3210_9876,543210"   failed: Input string was not in a corre

NumberStyles is not used; [nl-NL] culture IFormatProvider is used:
  Parse of "9876543210.9876543210"      succeeded: 98765432109876543210
  Parse of "9876543210,9876543210"      succeeded: 9876543210.9876543210
  Parse of "(9876543210,9876543210)"    failed: Input string was not in a corre
  Parse of "9,876,543,210,987,654.3210" failed: Input string was not in a corre
  Parse of "9.876.543.210.987.654,3210" succeeded: 9876543210987654.3210
  Parse of "98_7654_3210_9876,543210"   failed: Input string was not in a corre

NumberStyles::Currency is used, group size = 4, separator = "_":
  Parse of "9876543210.9876543210"      succeeded: 98765432109876543210
  Parse of "9876543210,9876543210"      succeeded: 9876543210.9876543210
  Parse of "(9876543210,9876543210)"    succeeded: -9876543210.9876543210
  Parse of "9,876,543,210,987,654.3210" failed: Input string was not in a corre
  Parse of "9.876.543.210.987.654,3210" succeeded: 9876543210987654.3210
  Parse of "98_7654_3210_9876,543210"   succeeded: 98765432109876.543210
*/

[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

参照

Decimal 構造体 | Decimal メンバ | System 名前空間 | Decimal.Parse オーバーロードの一覧 | 書式設定の概要 | ToString | Math.Round