Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
ToDecimal Method (String)

Convert.ToDecimal Method (String)

.NET Framework 1.1

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

[Visual Basic]
Overloads Public Shared Function ToDecimal( _
   ByVal value As String _
) As Decimal
[C#]
public static decimal ToDecimal(
 string value
);
[C++]
public: static Decimal ToDecimal(
 String* value
);
[JScript]
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).

Exceptions

Exception Type Condition
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.

Remarks

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

Example

[Visual Basic, C#, C++] 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.

[Visual Basic] 
Public Sub ConvertStringDecimal(ByVal stringVal As String)
    Dim decimalVal As Decimal = 0

    Try
        decimalVal = System.Convert.ToDecimal(stringVal)
        System.Console.WriteLine("The string as a decimal is {0}.", _
                                  decimalVal)
    Catch exception As System.OverflowException
        System.Console.WriteLine( _
            "Overflow in string-to-decimal conversion.")
    Catch exception As System.FormatException
        System.Console.WriteLine( _
            "The string is not formatted as a decimal.")
    Catch exception As System.ArgumentException
        System.Console.WriteLine("The string is null.")
    End Try

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

[C#] 
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);
}    

[C++] 

   void ConvertStringDecimal(String* stringVal)
   {
      Decimal decimalVal = 0;

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

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

[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.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Convert Class | Convert Members | System Namespace | Convert.ToDecimal Overload List

Show:
© 2015 Microsoft