Export (0) Print
Expand All

Decimal.Parse Method

Converts the String representation of a number to its Decimal equivalent.

Overload List

Converts the String representation of a number to its Decimal equivalent.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Parse(String) As Decimal
[C#] public static decimal Parse(string);
[C++] public: static Decimal Parse(String*);
[JScript] public static function Parse(String) : Decimal;

Converts the String representation of a number to its Decimal equivalent using the specified culture-specific format information.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Parse(String, IFormatProvider) As Decimal
[C#] public static decimal Parse(string, IFormatProvider);
[C++] public: static Decimal Parse(String*, IFormatProvider*);
[JScript] public static function Parse(String, IFormatProvider) : Decimal;

Converts the String representation of a number in a specified style to its Decimal equivalent using the specified formatting style.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Parse(String, NumberStyles) As Decimal
[C#] public static decimal Parse(string, NumberStyles);
[C++] public: static Decimal Parse(String*, NumberStyles);
[JScript] public static function Parse(String, NumberStyles) : Decimal;

Converts the String representation of a number to its Decimal equivalent using the specified style and culture-specific format.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Parse(String, NumberStyles, IFormatProvider) As Decimal
[C#] public static decimal Parse(string, NumberStyles, IFormatProvider);
[C++] public: static Decimal Parse(String*, NumberStyles, IFormatProvider*);
[JScript] public static function Parse(String, NumberStyles, IFormatProvider) : Decimal;

Example

[Visual Basic, C#, C++] The following code example parses String representations of Decimal values with several overloads of the Parse method.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Parse. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
' 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] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Decimal Structure | Decimal Members | System Namespace

Show:
© 2014 Microsoft