Export (0) Print
Expand All

Convert.ToDecimal Method

Converts a specified value to a Decimal number.

Overload List

Converts the value of the specified Boolean value to the equivalent Decimal number.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDecimal(Boolean) As Decimal
[C#] public static decimal ToDecimal(bool);
[C++] public: static Decimal ToDecimal(bool);
[JScript] public static function ToDecimal(Boolean) : Decimal;

Converts the value of the specified 8-bit unsigned integer to the equivalent Decimal number.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDecimal(Byte) As Decimal
[C#] public static decimal ToDecimal(byte);
[C++] public: static Decimal ToDecimal(unsigned char);
[JScript] public static function ToDecimal(Byte) : Decimal;

Calling this method always throws InvalidCastException.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDecimal(Char) As Decimal
[C#] public static decimal ToDecimal(char);
[C++] public: static Decimal ToDecimal(__wchar_t);
[JScript] public static function ToDecimal(Char) : Decimal;

Calling this method always throws InvalidCastException.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDecimal(DateTime) As Decimal
[C#] public static decimal ToDecimal(DateTime);
[C++] public: static Decimal ToDecimal(DateTime);
[JScript] public static function ToDecimal(DateTime) : Decimal;

Returns the specified Decimal number; no actual conversion is performed.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDecimal(Decimal) As Decimal
[C#] public static decimal ToDecimal(decimal);
[C++] public: static Decimal ToDecimal(Decimal);
[JScript] public static function ToDecimal(Decimal) : Decimal;

Converts the value of the specified double-precision floating point number to an equivalent Decimal number.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDecimal(Double) As Decimal
[C#] public static decimal ToDecimal(double);
[C++] public: static Decimal ToDecimal(double);
[JScript] public static function ToDecimal(double) : Decimal;

Converts the value of the specified 16-bit signed integer to an equivalent Decimal number.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDecimal(Short) As Decimal
[C#] public static decimal ToDecimal(short);
[C++] public: static Decimal ToDecimal(short);
[JScript] public static function ToDecimal(Int16) : Decimal;

Converts the value of the specified 32-bit signed integer to an equivalent Decimal number.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDecimal(Integer) As Decimal
[C#] public static decimal ToDecimal(int);
[C++] public: static Decimal ToDecimal(int);
[JScript] public static function ToDecimal(int) : Decimal;

Converts the value of the specified 64-bit signed integer to an equivalent Decimal number.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDecimal(Long) As Decimal
[C#] public static decimal ToDecimal(long);
[C++] public: static Decimal ToDecimal(__int64);
[JScript] public static function ToDecimal(long) : Decimal;

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDecimal(Object) As Decimal
[C#] public static decimal ToDecimal(object);
[C++] public: static Decimal ToDecimal(Object*);
[JScript] public static function ToDecimal(Object) : Decimal;

Converts the value of the specified 8-bit signed integer to the equivalent Decimal number. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDecimal(SByte) As Decimal
[C#] public static decimal ToDecimal(sbyte);
[C++] public: static Decimal ToDecimal(char);
[JScript] public static function ToDecimal(SByte) : Decimal;

Converts the value of the specified single-precision floating point number to the equivalent Decimal number.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDecimal(Single) As Decimal
[C#] public static decimal ToDecimal(float);
[C++] public: static Decimal ToDecimal(float);
[JScript] public static function ToDecimal(float) : Decimal;

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDecimal(String) As Decimal
[C#] public static decimal ToDecimal(string);
[C++] public: static Decimal ToDecimal(String*);
[JScript] public static function ToDecimal(String) : Decimal;

Converts the value of the specified 16-bit unsigned integer to the equivalent Decimal number. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDecimal(UInt16) As Decimal
[C#] public static decimal ToDecimal(ushort);
[C++] public: static Decimal ToDecimal(unsigned short);
[JScript] public static function ToDecimal(UInt16) : Decimal;

Converts the value of the specified 32-bit unsigned integer to an equivalent Decimal number. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDecimal(UInt32) As Decimal
[C#] public static decimal ToDecimal(uint);
[C++] public: static Decimal ToDecimal(unsigned int);
[JScript] public static function ToDecimal(UInt32) : Decimal;

Converts the value of the specified 64-bit unsigned integer to an equivalent Decimal number. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDecimal(UInt64) As Decimal
[C#] public static decimal ToDecimal(ulong);
[C++] public: static Decimal ToDecimal(unsigned __int64);
[JScript] public static function ToDecimal(UInt64) : Decimal;

Converts the value of the specified Object to an Decimal number using the specified culture-specific formatting information.

[Visual Basic] Overloads Public Shared Function ToDecimal(Object, IFormatProvider) As Decimal
[C#] public static decimal ToDecimal(object, IFormatProvider);
[C++] public: static Decimal ToDecimal(Object*, IFormatProvider*);
[JScript] public static function ToDecimal(Object, IFormatProvider) : Decimal;

Converts the specified String representation of a number to an equivalent Decimal number using the specified culture-specific formatting information.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ToDecimal(String, IFormatProvider) As Decimal
[C#] public static decimal ToDecimal(string, IFormatProvider);
[C++] public: static Decimal ToDecimal(String*, IFormatProvider*);
[JScript] public static function ToDecimal(String, IFormatProvider) : Decimal;

Example

[Visual Basic, C#, C++] The following code example converts String representations of Decimal values with the ToDecimal method, using an IFormatProvider object.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of ToDecimal. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
' Example of the Convert.ToDecimal( String ) and 
' Convert.ToDecimal( String, IFormatProvider ) methods.
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic

Module ToDecimalProviderDemo

    Dim formatter As String = "{0,-22}{1,-20}{2}"

    ' Get the exception type name; remove the namespace prefix.
    Function GetExceptionType( ex As Exception ) As String

        Dim exceptionType   As String = ex.GetType( ).ToString( )
        GetExceptionType = exceptionType.Substring( _
            exceptionType.LastIndexOf( "."c ) + 1 )
    End Function

    Sub ConvertToDecimal( numericStr As String, _
        provider As IFormatProvider )

        Dim defaultValue    As Object
        Dim providerValue   As Object

        ' Convert numericStr to Decimal without a format provider.
        Try
            defaultValue = Convert.ToDecimal( numericStr )
        Catch ex As Exception
            defaultValue = GetExceptionType( ex )
        End Try

        ' Convert numericStr to Decimal with a format provider.
        Try
            providerValue = Convert.ToDecimal( numericStr, provider )
        Catch ex As Exception
            providerValue = GetExceptionType( ex )
        End Try

        Console.WriteLine( formatter, numericStr, _
            defaultValue, providerValue )
    End Sub

    Sub Main( )

        ' Create a NumberFormatInfo object and set several of its
        ' properties that apply to numbers.
        Dim provider  As NumberFormatInfo = new NumberFormatInfo( )

        provider.NumberDecimalSeparator = ","
        provider.NumberGroupSeparator = "."
        provider.NumberGroupSizes = New Integer( ) { 3 }

        Console.WriteLine( "This example of" & vbCrLf & _
            "  Convert.ToDecimal( String ) and " & vbCrLf & _
            "  Convert.ToDecimal( String, IFormatProvider ) " & _
            vbCrLf & "generates the " & _
            "following output when run in the [{0}] culture.", _
            CultureInfo.CurrentCulture.Name )
        Console.WriteLine( vbCrLf & _
            "Several strings are converted to Decimal values, " & _
            "using " & vbCrLf & "default formatting " & _
            "and a NumberFormatInfo object." & vbCrLf )
        Console.WriteLine( formatter, "String to convert", _
            "Default/exception", "Provider/exception" )
        Console.WriteLine( formatter, "-----------------", _
            "-----------------", "------------------" )

        ' Convert strings, with and without an IFormatProvider.
        ConvertToDecimal( "123456789", provider )
        ConvertToDecimal( "12345.6789", provider )
        ConvertToDecimal( "12345,6789", provider )
        ConvertToDecimal( "123,456.789", provider )
        ConvertToDecimal( "123.456,789", provider )
        ConvertToDecimal( "123,456,789.0123", provider )
        ConvertToDecimal( "123.456.789,0123", provider )
    End Sub 
End Module 

' This example of
'   Convert.ToDecimal( String ) and
'   Convert.ToDecimal( String, IFormatProvider )
' generates the following output when run in the [en-US] culture.
' 
' Several strings are converted to Decimal values, using
' default formatting and a NumberFormatInfo object.
' 
' String to convert     Default/exception   Provider/exception
' -----------------     -----------------   ------------------
' 123456789             123456789           123456789
' 12345.6789            12345.6789          123456789
' 12345,6789            123456789           12345.6789
' 123,456.789           123456.789          FormatException
' 123.456,789           FormatException     123456.789
' 123,456,789.0123      123456789.0123      FormatException
' 123.456.789,0123      FormatException     123456789.0123

[C#] 
// Example of the Convert.ToDecimal( String ) and 
// Convert.ToDecimal( String, IFormatProvider ) methods.
using System;
using System.Globalization;

class ToDecimalProviderDemo
{
    static string formatter = "{0,-22}{1,-20}{2}";

    // Get the exception type name; remove the namespace prefix.
    static string GetExceptionType( Exception ex )
    {
        string exceptionType = ex.GetType( ).ToString( );
        return exceptionType.Substring( 
            exceptionType.LastIndexOf( '.' ) + 1 );
    }

    static void ConvertToDecimal( string numericStr, 
        IFormatProvider provider )
    {
        object defaultValue;
        object providerValue;

        // Convert numericStr to decimal without a format provider.
        try
        {
            defaultValue = Convert.ToDecimal( numericStr );
        }
        catch( Exception ex )
        {
            defaultValue = GetExceptionType( ex );
        }

        // Convert numericStr to decimal with a format provider.
        try
        {
            providerValue = Convert.ToDecimal( numericStr, provider );
        }
        catch( Exception ex )
        {
            providerValue = GetExceptionType( ex );
        }

        Console.WriteLine( formatter, numericStr, defaultValue, 
            providerValue );
    }

    static void Main( )
    {
        // Create a NumberFormatInfo object and set several of its
        // properties that apply to numbers.
        NumberFormatInfo provider = new NumberFormatInfo( );

        provider.NumberDecimalSeparator = ",";
        provider.NumberGroupSeparator = ".";
        provider.NumberGroupSizes = new int[ ] { 3 };

        Console.WriteLine( 
            "This example of\n  Convert.ToDecimal( String ) and \n" +
            "  Convert.ToDecimal( String, IFormatProvider ) \n" +
            "generates the following output when run in the " +
            "[{0}] culture.", 
            CultureInfo.CurrentCulture.Name );
        Console.WriteLine( "\nSeveral " +
            "strings are converted to decimal values, using \n" +
            "default formatting and a NumberFormatInfo object.\n");
        Console.WriteLine( formatter, "String to convert", 
            "Default/exception", "Provider/exception" );
        Console.WriteLine( formatter, "-----------------", 
            "-----------------", "------------------" );

        // Convert strings, with and without an IFormatProvider.
        ConvertToDecimal( "123456789", provider );
        ConvertToDecimal( "12345.6789", provider );
        ConvertToDecimal( "12345,6789", provider );
        ConvertToDecimal( "123,456.789", provider );
        ConvertToDecimal( "123.456,789", provider );
        ConvertToDecimal( "123,456,789.0123", provider );
        ConvertToDecimal( "123.456.789,0123", provider );
    }
}

/*
This example of
  Convert.ToDecimal( String ) and
  Convert.ToDecimal( String, IFormatProvider )
generates the following output when run in the [en-US] culture.

Several strings are converted to decimal values, using
default formatting and a NumberFormatInfo object.

String to convert     Default/exception   Provider/exception
-----------------     -----------------   ------------------
123456789             123456789           123456789
12345.6789            12345.6789          123456789
12345,6789            123456789           12345.6789
123,456.789           123456.789          FormatException
123.456,789           FormatException     123456.789
123,456,789.0123      123456789.0123      FormatException
123.456.789,0123      FormatException     123456789.0123
*/

[C++] 
// Example of the Convert::ToDecimal( String ) and 
// Convert::ToDecimal( String, IFormatProvider ) methods.
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;

const __wchar_t* formatter = L"{0,-22}{1,-20}{2}";

// Get the exception type name; remove the namespace prefix.
String* GetExceptionType( Exception* ex )
{
    String* exceptionType = ex->GetType( )->ToString( );
    return exceptionType->Substring( 
        exceptionType->LastIndexOf( '.' ) + 1 );
}

void ConvertToDecimal( String* numericStr, 
    IFormatProvider* provider )
{
    Object* defaultValue;
    Object* providerValue;

    // Convert numericStr to Decimal without a format provider.
    try
    {
        defaultValue = __box( Convert::ToDecimal( numericStr ) );
    }
    catch( Exception* ex )
    {
        defaultValue = GetExceptionType( ex );
    }

    // Convert numericStr to Decimal with a format provider.
    try
    {
        providerValue = __box( 
            Convert::ToDecimal( numericStr, provider ) );
    }
    catch( Exception* ex )
    {
        providerValue = GetExceptionType( ex );
    }

    Console::WriteLine( new String( formatter ), numericStr, 
        defaultValue, providerValue );
}

void main( )
{
    // Create a NumberFormatInfo object and set several of its
    // properties that apply to numbers.
    NumberFormatInfo* provider = new NumberFormatInfo( );

    provider->NumberDecimalSeparator = S",";
    provider->NumberGroupSeparator = S".";
    Int32 sizes __gc [] = { 3 };
    provider->NumberGroupSizes = sizes;

    Console::WriteLine( 
        S"This example of\n  Convert::ToDecimal( String* ) and \n" 
        S"  Convert::ToDecimal( String*, IFormatProvider* ) \n" 
        S"generates the following output when run in the " 
        S"[{0}] culture.", 
        CultureInfo::CurrentCulture->Name );
    Console::WriteLine( S"\nSeveral " 
        S"strings are converted to Decimal values, using \n" 
        S"default formatting and a NumberFormatInfo object.\n");
    Console::WriteLine( new String( formatter ), S"String to convert", 
        S"Default/exception", S"Provider/exception" );
    Console::WriteLine( new String( formatter ), S"-----------------", 
        S"-----------------", S"------------------" );

    // Convert strings, with and without an IFormatProvider.
    ConvertToDecimal( S"123456789", provider );
    ConvertToDecimal( S"12345.6789", provider );
    ConvertToDecimal( S"12345,6789", provider );
    ConvertToDecimal( S"123,456.789", provider );
    ConvertToDecimal( S"123.456,789", provider );
    ConvertToDecimal( S"123,456,789.0123", provider );
    ConvertToDecimal( S"123.456.789,0123", provider );
}

/*
This example of
  Convert::ToDecimal( String* ) and
  Convert::ToDecimal( String*, IFormatProvider* )
generates the following output when run in the [en-US] culture.

Several strings are converted to Decimal values, using
default formatting and a NumberFormatInfo object.

String to convert     Default/exception   Provider/exception
-----------------     -----------------   ------------------
123456789             123456789           123456789
12345.6789            12345.6789          123456789
12345,6789            123456789           12345.6789
123,456.789           123456.789          FormatException
123.456,789           FormatException     123456.789
123,456,789.0123      123456789.0123      FormatException
123.456.789,0123      FormatException     123456789.0123
*/

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

See Also

Convert Class | Convert Members | System Namespace

Show:
© 2014 Microsoft