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

Convert.ToDecimal Method (Object, IFormatProvider)

Converts the value of the specified object to an equivalent decimal number, using the specified culture-specific formatting information.

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

public static decimal ToDecimal(
	Object value,
	IFormatProvider provider


Type: System.Object

An object that implements the IConvertible interface.

Type: System.IFormatProvider

An object that supplies culture-specific formatting information.

Return Value

Type: System.Decimal
A decimal number that is equivalent to value, or 0 (zero) if value is null.


value is not in an appropriate format for a Decimal type.


value does not implement the IConvertible interface.


The conversion is not supported.


value represents a number that is less than Decimal.MinValue or greater than Decimal.MaxValue.

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

provider enables the user to specify culture-specific conversion information about the contents of value. The base types ignore provider; however, the parameter may be used if value is a user-defined type that implements the IConvertible interface.

The following example defines a Temperature class that implements the IConvertible interface.

using System;
using System.Globalization;

public class Temperature : IConvertible
   private decimal m_Temp;

   public Temperature(decimal temperature)
      this.m_Temp = temperature;

   public decimal Celsius
      get { return this.m_Temp; }   

   public decimal Kelvin
      get { return this.m_Temp + 273.15m; }    

   public decimal Fahrenheit
      get { return Math.Round((decimal) (this.m_Temp * 9 / 5 + 32), 2); }

   public override string ToString()
      return m_Temp.ToString("N2") + " °C";

   // IConvertible implementations. 
   public TypeCode GetTypeCode()
      return TypeCode.Object;

   public bool ToBoolean(IFormatProvider provider) 
      if (m_Temp == 0)
         return false;
         return true;

   public byte ToByte(IFormatProvider provider)
      if (m_Temp < Byte.MinValue || m_Temp > Byte.MaxValue)
         throw new OverflowException(String.Format("{0} is out of range of the Byte type.", 
         return Decimal.ToByte(this.m_Temp);

   public char ToChar(IFormatProvider provider)
      throw new InvalidCastException("Temperature to Char conversion is not supported.");

   public DateTime ToDateTime(IFormatProvider provider)
      throw new InvalidCastException("Temperature to DateTime conversion is not supported.");

   public decimal ToDecimal(IFormatProvider provider)
      return this.m_Temp;

   public double ToDouble(IFormatProvider provider)
      return Decimal.ToDouble(this.m_Temp);

   public short ToInt16(IFormatProvider provider)
      if (this.m_Temp < Int16.MinValue || this.m_Temp > Int16.MaxValue)
         throw new OverflowException(String.Format("{0} is out of range of the Int16 type.",
         return Decimal.ToInt16(this.m_Temp);

   public int ToInt32(IFormatProvider provider)
      if (this.m_Temp < Int32.MinValue || this.m_Temp > Int32.MaxValue)
         throw new OverflowException(String.Format("{0} is out of range of the Int32 type.",
         return Decimal.ToInt32(this.m_Temp);

   public long ToInt64(IFormatProvider provider)
      if (this.m_Temp < Int64.MinValue || this.m_Temp > Int64.MaxValue)
         throw new OverflowException(String.Format("{0} is out of range of the Int64 type.",
         return Decimal.ToInt64(this.m_Temp);

   public sbyte ToSByte(IFormatProvider provider)
      if (this.m_Temp < SByte.MinValue || this.m_Temp > SByte.MaxValue)
         throw new OverflowException(String.Format("{0} is out of range of the SByte type.",
         return Decimal.ToSByte(this.m_Temp);

   public float ToSingle(IFormatProvider provider)
      return Decimal.ToSingle(this.m_Temp);

   public string ToString(IFormatProvider provider)
      return m_Temp.ToString("N2", provider) + " °C";

   public object ToType(Type conversionType, IFormatProvider provider)
      switch (Type.GetTypeCode(conversionType))
         case TypeCode.Boolean: 
            return this.ToBoolean(null);
         case TypeCode.Byte:
            return this.ToByte(null);
         case TypeCode.Char:
            return this.ToChar(null);
         case TypeCode.DateTime:
            return this.ToDateTime(null);
         case TypeCode.Decimal:
            return this.ToDecimal(null);
         case TypeCode.Double:
            return this.ToDouble(null);
         case TypeCode.Int16:
            return this.ToInt16(null);
         case TypeCode.Int32:
            return this.ToInt32(null);
         case TypeCode.Int64:
            return this.ToInt64(null);
         case TypeCode.Object:
            if (typeof(Temperature).Equals(conversionType))
               return this;
               throw new InvalidCastException(String.Format("Conversion to a {0} is not supported.",
         case TypeCode.SByte:
            return this.ToSByte(null);
         case TypeCode.Single:
            return this.ToSingle(null);
         case TypeCode.String:
            return this.ToString(provider);
         case TypeCode.UInt16:
            return this.ToUInt16(null);
         case TypeCode.UInt32:
            return this.ToUInt32(null);
         case TypeCode.UInt64:
            return this.ToUInt64(null);   
            throw new InvalidCastException(String.Format("Conversion to {0} is not supported.", conversionType.Name));   

   public ushort ToUInt16(IFormatProvider provider)
      if (this.m_Temp < UInt16.MinValue || this.m_Temp > UInt16.MaxValue)
         throw new OverflowException(String.Format("{0} is out of range of the UInt16 type.",
         return Decimal.ToUInt16(this.m_Temp);

   public uint ToUInt32(IFormatProvider provider)
      if (this.m_Temp < UInt32.MinValue || this.m_Temp > UInt32.MaxValue)
         throw new OverflowException(String.Format("{0} is out of range of the UInt32 type.",
         return Decimal.ToUInt32(this.m_Temp);

   public ulong ToUInt64(IFormatProvider provider)
      if (this.m_Temp < UInt64.MinValue || this.m_Temp > UInt64.MaxValue)
         throw new OverflowException(String.Format("{0} is out of range of the UInt64 type.",
         return Decimal.ToUInt64(this.m_Temp);

The following example shows that when a Temperature object is passed as a parameter to the ToDecimal(Object, IFormatProvider) method, the IConvertible.ToDecimal implementation of the Temperature class is called to perform the conversion.

public class Example
   public static void Main()
      Temperature cold = new Temperature(-40);
      Temperature freezing = new Temperature(0);
      Temperature boiling = new Temperature(100);

      Console.WriteLine(Convert.ToDecimal(cold, null));
      Console.WriteLine(Convert.ToDecimal(freezing, null));
      Console.WriteLine(Convert.ToDecimal(boiling, null));
// The example dosplays the following output: 
//       -40 
//       0 
//       100

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

© 2015 Microsoft