Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Convert.ToDecimal Method (Object)

Converts the value of the specified Object to a Decimal number.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public static decimal ToDecimal(
	Object value
)

Parameters

value
Type: System.Object
An Object that implements the IConvertible interface or null.

Return Value

Type: System.Decimal
A Decimal number equivalent to the value of value, or zero if value is null.
ExceptionCondition
InvalidCastException

value does not implement IConvertible.

The return value is the result of invoking the IConvertible.ToDecimal method of the underlying type of 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);
      outputBlock.Text += String.Format(
         "The string as a decimal is {0}.", decimalVal) + "\n";
   }
   catch (System.OverflowException)
   {
      outputBlock.Text += String.Format(
         "The conversion from string to decimal overflowed.") + "\n";
   }
   catch (System.FormatException)
   {
      outputBlock.Text += String.Format(
         "The string is not formatted as a decimal.") + "\n";
   }
   catch (System.ArgumentNullException)
   {
      outputBlock.Text += String.Format(
         "The string is null.") + "\n";
   }

   // Decimal to string conversion will not overflow.
   stringVal = System.Convert.ToString(decimalVal);
   outputBlock.Text += String.Format(
      "The decimal as a string is {0}.", stringVal) + "\n";
}


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.