Compartir a través de


Convert.ToDecimal Method (Object)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Shared Function ToDecimal ( _
    value As Object _
) As Decimal
public static decimal ToDecimal(
    Object value
)

Parameters

Return Value

Type: System.Decimal
A Decimal number equivalent to the value of value, or zero if value is nulla null reference (Nothing in Visual Basic).

Exceptions

Exception Condition
InvalidCastException

value does not implement IConvertible.

Remarks

The return value is the result of invoking the IConvertible.ToDecimal method of the underlying type of value.

Examples

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 Sub ConvertStringDecimal(ByVal stringVal As String)
   Dim decimalVal As Decimal = 0

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

   ' Decimal to string conversion will not overflow.
   stringVal = System.Convert.ToString(decimalVal)
   outputBlock.Text &= String.Format("The decimal as a string is {0}.", _
                             stringVal) & vbCrLf
End Sub
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";
}

Version Information

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

Platforms

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