Export (0) Print
Expand All

Convert.ToDecimal Method (String, IFormatProvider)

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

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public static decimal ToDecimal (
	string value,
	IFormatProvider provider
)
public static Decimal ToDecimal (
	String value, 
	IFormatProvider provider
)
public static function ToDecimal (
	value : String, 
	provider : IFormatProvider
) : decimal

Parameters

value

A String containing a number to convert.

provider

An IFormatProvider interface implementation that supplies culture-specific formatting information.

Return Value

A Decimal number equivalent to the value of value. -or- Zero if value is a null reference (Nothing in Visual Basic).

Exception typeCondition

FormatException

value does not consist of an optional sign followed by a sequence of digits (zero through nine) and an optional decimal point symbol.

OverflowException

value represents a number less than MinValue or greater than MaxValue.

The return value is the result of invoking the Decimal.Parse method on value.

provider is an IFormatProvider instance that obtains a NumberFormatInfo object. The NumberFormatInfo object provides culture-specific information about the format of value. If provider is a null reference (Nothing in Visual Basic), the NumberFormatInfo for the current culture is used.

The following code example converts String representations of Decimal values with the ToDecimal method, using an IFormatProvider object.

// Example of the Convert.ToDecimal( String ) and 
// Convert.ToDecimal( String, IFormatProvider ) methods.
using System;
using System.Globalization;

class ToDecimalProviderDemo
{
    static string formatter = "{0,-22}{1,-20}{2}";

    // 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 );
    }

    static void ConvertToDecimal( string numericStr, 
        IFormatProvider provider )
    {
        object defaultValue;
        object providerValue;

        // Convert numericStr to decimal without a format provider.
        try
        {
            defaultValue = Convert.ToDecimal( numericStr );
        }
        catch( Exception ex )
        {
            defaultValue = GetExceptionType( ex );
        }

        // Convert numericStr to decimal with a format provider.
        try
        {
            providerValue = Convert.ToDecimal( numericStr, provider );
        }
        catch( Exception ex )
        {
            providerValue = GetExceptionType( ex );
        }

        Console.WriteLine( formatter, numericStr, defaultValue, 
            providerValue );
    }

    static void Main( )
    {
        // Create a NumberFormatInfo object and set several of its
        // properties that apply to numbers.
        NumberFormatInfo provider = new NumberFormatInfo( );

        provider.NumberDecimalSeparator = ",";
        provider.NumberGroupSeparator = ".";
        provider.NumberGroupSizes = new int[ ] { 3 };

        Console.WriteLine( 
            "This example of\n  Convert.ToDecimal( String ) and \n" +
            "  Convert.ToDecimal( String, IFormatProvider ) \n" +
            "generates the following output when run in the " +
            "[{0}] culture.", 
            CultureInfo.CurrentCulture.Name );
        Console.WriteLine( "\nSeveral " +
            "strings are converted to decimal values, using \n" +
            "default formatting and a NumberFormatInfo object.\n");
        Console.WriteLine( formatter, "String to convert", 
            "Default/exception", "Provider/exception" );
        Console.WriteLine( formatter, "-----------------", 
            "-----------------", "------------------" );

        // Convert strings, with and without an IFormatProvider.
        ConvertToDecimal( "123456789", provider );
        ConvertToDecimal( "12345.6789", provider );
        ConvertToDecimal( "12345,6789", provider );
        ConvertToDecimal( "123,456.789", provider );
        ConvertToDecimal( "123.456,789", provider );
        ConvertToDecimal( "123,456,789.0123", provider );
        ConvertToDecimal( "123.456.789,0123", provider );
    }
}

/*
This example of
  Convert.ToDecimal( String ) and
  Convert.ToDecimal( String, IFormatProvider )
generates the following output when run in the [en-US] culture.

Several strings are converted to decimal values, using
default formatting and a NumberFormatInfo object.

String to convert     Default/exception   Provider/exception
-----------------     -----------------   ------------------
123456789             123456789           123456789
12345.6789            12345.6789          123456789
12345,6789            123456789           12345.6789
123,456.789           123456.789          FormatException
123.456,789           FormatException     123456.789
123,456,789.0123      123456789.0123      FormatException
123.456.789,0123      FormatException     123456789.0123
*/

// Example of the Convert.ToDecimal( String ) and 
// Convert.ToDecimal( String, IFormatProvider ) methods.
import System.* ;
import System.Globalization.* ;

class ToDecimalProviderDemo
{
    private static String formatter = "{0,-22}{1,-20}{2}";
   
    // Get the exception type name; remove the namespace prefix.
    static String GetExceptionType(System.Exception ex)
    {
        String exceptionType = ex.GetType().ToString();
        return exceptionType.Substring((exceptionType.LastIndexOf('.') + 1)) ;
    } //GetExceptionType   
   
    static void ConvertToDecimal(String numericStr, IFormatProvider provider)
    {
        Object defaultValue;
        Object providerValue;
      
        // Convert numericStr to decimal without a format provider.
        try {
            defaultValue = Convert.ToDecimal(numericStr);
        }
        catch(System.Exception  ex){      
            defaultValue = GetExceptionType(ex);
        }
      
        // Convert numericStr to decimal with a format provider.
        try {
            providerValue = Convert.ToDecimal(numericStr, provider);
        }
        catch(System.Exception  ex){
            providerValue = GetExceptionType(ex);
        }
      
        Console.WriteLine(formatter, numericStr, defaultValue, providerValue);
    } //ConvertToDecimal
      
    public static void main(String[] args)
    {
        // Create a NumberFormatInfo object and set several of its
        // properties that apply to numbers.
        NumberFormatInfo provider =  new NumberFormatInfo();
      
        provider.set_NumberDecimalSeparator(",");
        provider.set_NumberGroupSeparator(".");
        provider.set_NumberGroupSizes(new int[]{3});
      
        Console.WriteLine(
            "This example of\n  Convert.ToDecimal( String ) and \n" 
            + "  Convert.ToDecimal( String, IFormatProvider ) \n" 
            + "generates the following output when run in the " 
            + "[{0}] culture.", CultureInfo.get_CurrentCulture().get_Name());
        Console.WriteLine(("\nSeveral " 
            + "strings are converted to decimal values, using \n" 
            + "default formatting and a NumberFormatInfo object.\n"));
        Console.WriteLine(formatter, "String to convert", 
            "Default/exception", "Provider/exception");
        Console.WriteLine(formatter, "-----------------", "-----------------", 
            "------------------");
      
        // Convert strings, with and without an IFormatProvider.
        ConvertToDecimal("123456789", provider);
        ConvertToDecimal("12345.6789", provider);
        ConvertToDecimal("12345,6789", provider);
        ConvertToDecimal("123,456.789", provider);
        ConvertToDecimal("123.456,789", provider);
        ConvertToDecimal("123,456,789.0123", provider);
        ConvertToDecimal("123.456.789,0123", provider);
    } //main
} //ToDecimalProviderDemo

/*
This example of
  Convert.ToDecimal( String ) and
  Convert.ToDecimal( String, IFormatProvider )
generates the following output when run in the [en-US] culture.

Several strings are converted to decimal values, using
default formatting and a NumberFormatInfo object.

String to convert     Default/exception   Provider/exception
-----------------     -----------------   ------------------
123456789             123456789           123456789
12345.6789            12345.6789          123456789
12345,6789            123456789           12345.6789
123,456.789           123456.789          FormatException
123.456,789           FormatException      123456.789
123,456,789.0123      123456789.0123      FormatException
123.456.789,0123      FormatException      123456789.0123
*/

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft