This documentation is archived and is not being maintained.

Convert.ToDecimal Method (String)

Converts the specified String representation of a number to an equivalent Decimal number.

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

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

Parameters

value

A String containing a number to convert.

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.

The following code sample illustrates the use of ToDecimal, by attempting to convert a String to a Decimal and throwing any of the possible exceptions that may arise during the conversion.

public void ConvertStringDecimal(string stringVal) {
    decimal decimalVal = 0;
    
    try {
        decimalVal = System.Convert.ToDecimal(stringVal);
        System.Console.WriteLine(
            "The string as a decimal is {0}.", decimalVal);
    } 
    catch (System.OverflowException){
        System.Console.WriteLine(
            "The conversion from string to decimal overflowed.");
    }
    catch (System.FormatException) {
        System.Console.WriteLine(
            "The string is not formatted as a decimal.");
    }
    catch (System.ArgumentNullException) {
        System.Console.WriteLine(
            "The string is null.");
    }

    // Decimal to string conversion will not overflow.
    stringVal = System.Convert.ToString(decimalVal);
    System.Console.WriteLine(
        "The decimal as a string is {0}.", stringVal);
}    

public void ConvertStringDecimal(String stringVal)
{
    System.Decimal decimalVal = System.Convert.ToDecimal(0);

    try {        
        decimalVal = System.Convert.ToDecimal(stringVal);
        System.Console.WriteLine("The string as a decimal is {0}.", 
            decimalVal);
    }
    catch (System.OverflowException exp) {
        System.Console.WriteLine(
            "The conversion from string to decimal overflowed.");
    }
    catch (System.FormatException exp) {        
        System.Console.WriteLine(
            "The string is not formatted as a decimal.");
    }
    catch (System.ArgumentNullException exp) {        
        System.Console.WriteLine("The string is null.");
    }

    // Decimal to string conversion will not overflow.
    stringVal = System.Convert.ToString(decimalVal);
    System.Console.WriteLine("The decimal as a string is {0}.", stringVal);
} //ConvertStringDecimal

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
Show: