Export (0) Print
Expand All

Decimal.ToString Method

Converts the numeric value of this instance to its equivalent String representation.

Overload List

Converts the numeric value of this instance to its equivalent String representation.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Overrides Public Function ToString() As String
[C#] public override string ToString();
[C++] public: String* ToString();
[JScript] public override function ToString() : String;

Converts the numeric value of this instance to its equivalent String representation using the specified culture-specific format information.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function ToString(IFormatProvider) As String
[C#] public virtual string ToString(IFormatProvider);
[C++] public: virtual String* ToString(IFormatProvider*);
[JScript] public function ToString(IFormatProvider) : String;

Converts the numeric value of this instance to its equivalent String representation, using the specified format.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function ToString(String) As String
[C#] public string ToString(string);
[C++] public: String* ToString(String*);
[JScript] public function ToString(String) : String;

Converts the numeric value of this instance to its equivalent String representation using the specified format and culture-specific format information.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function ToString(String, IFormatProvider) As String
[C#] public virtual string ToString(string, IFormatProvider);
[C++] public: virtual String* ToString(String*, IFormatProvider*);
[JScript] public function ToString(String, IFormatProvider) : String;

Example

[Visual Basic, C#, C++] The following code example formats a Decimal number String with several overloads of the ToString method.

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

Module DecimalToStringDemo
    
    Sub Main( )

        Dim nineBillPlus As Decimal = 9876543210.9876543210D
            
        Console.WriteLine( "This example of" & vbCrLf & _
            "   Decimal.ToString( ), " & vbCrLf & _
            "   Decimal.ToString( String )," & vbCrLf & _
            "   Decimal.ToString( IFormatProvider ), and " & _ 
            vbCrLf & "   Decimal.ToString( String, IFormat" & _
            "Provider )" & vbCrLf & "generates the following " & _
            "output when run in the [{0}] culture." & vbCrLf & _
            "Decimal numbers are formatted with various " & _
            "combinations " & vbCrLf & _
            "of format strings and IFormatProvider.", _
            CultureInfo.CurrentCulture.Name )

        ' Format the number without and with format strings.
        Console.WriteLine( vbCrLf & "IFormatProvider is not " & _
            "used; the default culture is [{0}]:", _
            CultureInfo.CurrentCulture.Name )
        Console.WriteLine( "   {0,-30}{1}", _
            "No format string:", nineBillPlus.ToString( ) )
        Console.WriteLine( "   {0,-30}{1}", _
            "'N' format string:", nineBillPlus.ToString( "N" ) )
        Console.WriteLine( "   {0,-30}{1}", _
            "'N5' format string:", nineBillPlus.ToString( "N5" ) )
            
        ' Create a CultureInfo object for another culture. Use
        ' [Dutch - The Netherlands] unless the current culture
        ' is Dutch language. In that case use [English - U.S.].
        Dim cultureName As String = IIf( _
            CultureInfo.CurrentCulture.Name.Substring( 0, 2 ) = _
                "nl", "en-US", "nl-NL" )
        Dim culture As New CultureInfo( cultureName )
            
        ' Use the CultureInfo object for an IFormatProvider.
        Console.WriteLine( vbCrLf & "A CultureInfo object " & _
            "for [{0}] is used for the IFormatProvider: ", _
            cultureName )
        Console.WriteLine( "   {0,-30}{1}", "No format string:", _
            nineBillPlus.ToString( culture ) )
        Console.WriteLine( "   {0,-30}{1}", _
            "'N5' format string:", _
            nineBillPlus.ToString( "N5", culture ) )
            
        ' Get the NumberFormatInfo object from CultureInfo, and
        ' then change the digit group size to 4 and the digit
        ' separator to '_'.
        Dim numInfo As NumberFormatInfo = culture.NumberFormat
        numInfo.NumberGroupSizes = New Integer( ) { 4 }
        numInfo.NumberGroupSeparator = "_"
            
        ' Use a NumberFormatInfo object for IFormatProvider.
        Console.WriteLine( vbCrLf & _
            "A NumberFormatInfo object with digit group " & _
            "size = 4 and " & vbCrLf & "digit separator " & _
            "= '_' is used for the IFormatProvider:" )
        Console.WriteLine( "   {0,-30}{1}", _
            "'N5' format string:", _
            nineBillPlus.ToString( "N5", culture ) )
    End Sub 
End Module 

' This example of
'    Decimal.ToString( ),
'    Decimal.ToString( String ),
'    Decimal.ToString( IFormatProvider ), and
'    Decimal.ToString( String, IFormatProvider )
' generates the following output when run in the [en-US] culture.
' Decimal numbers are formatted with various combinations
' of format strings and IFormatProvider.
' 
' IFormatProvider is not used; the default culture is [en-US]:
'    No format string:             9876543210.987654321
'    'N' format string:            9,876,543,210.99
'    'N5' format string:           9,876,543,210.98765
' 
' A CultureInfo object for [nl-NL] is used for the IFormatProvider:
'    No format string:             9876543210,987654321
'    'N5' format string:           9.876.543.210,98765
' 
' A NumberFormatInfo object with digit group size = 4 and
' digit separator = '_' is used for the IFormatProvider:
'    'N5' format string:           98_7654_3210,98765

[C#] 
// Example for the Decimal.ToString( ) methods.
using System;
using System.Globalization;
using Microsoft.VisualBasic;

class DecimalToStringDemo
{
    static void Main( )
    {
        decimal nineBillPlus = 9876543210.9876543210M;
            
        Console.WriteLine( "This example of\n" +
            "   Decimal.ToString( ), \n" +
            "   Decimal.ToString( String ),\n" +
            "   Decimal.ToString( IFormatProvider ), and \n" +
            "   Decimal.ToString( String, IFormatProvider )\n" +
            "generates the following output when run in the " +
            "[{0}] culture.\nDecimal numbers are formatted " +
            "with various combinations \nof format strings " +
            "and IFormatProvider.", 
            CultureInfo.CurrentCulture.Name );

        // Format the number without and with format strings.
        Console.WriteLine( "\nIFormatProvider is not " +
            "used; the default culture is [{0}]:", 
            CultureInfo.CurrentCulture.Name );
        Console.WriteLine( "   {0,-30}{1}", "No format string:", 
            nineBillPlus.ToString( ) );
        Console.WriteLine( "   {0,-30}{1}", "'N' format string:", 
            nineBillPlus.ToString( "N" ) );
        Console.WriteLine( "   {0,-30}{1}", "'N5' format string:", 
            nineBillPlus.ToString( "N5" ) );
            
        // Create a CultureInfo object for another culture. Use
        // [Dutch - The Netherlands] unless the current culture
        // is Dutch language. In that case use [English - U.S.].
        string cultureName = 
            CultureInfo.CurrentCulture.Name.Substring( 0, 2 ) == 
            "nl" ? "en-US" : "nl-NL";
        CultureInfo culture = new CultureInfo( cultureName );
            
        // Use the CultureInfo object for an IFormatProvider.
        Console.WriteLine( "\nA CultureInfo object " +
            "for [{0}] is used for the IFormatProvider: ", 
            cultureName );
        Console.WriteLine( "   {0,-30}{1}", "No format string:", 
            nineBillPlus.ToString( culture ) );
        Console.WriteLine( "   {0,-30}{1}", "'N5' format string:", 
            nineBillPlus.ToString( "N5", culture ) );
            
        // Get the NumberFormatInfo object from CultureInfo, and
        // then change the digit group size to 4 and the digit
        // separator to '_'.
        NumberFormatInfo numInfo = culture.NumberFormat;
        numInfo.NumberGroupSizes = new int[ ] { 4 };
        numInfo.NumberGroupSeparator = "_";
            
        // Use a NumberFormatInfo object for IFormatProvider.
        Console.WriteLine( 
            "\nA NumberFormatInfo object with digit group " +
            "size = 4 and \ndigit separator " +
            "= '_' is used for the IFormatProvider:" );
        Console.WriteLine( "   {0,-30}{1}", "'N5' format string:", 
            nineBillPlus.ToString( "N5", culture ) );
    } 
} 

/*
This example of
   Decimal.ToString( ),
   Decimal.ToString( String ),
   Decimal.ToString( IFormatProvider ), and
   Decimal.ToString( String, IFormatProvider )
generates the following output when run in the [en-US] culture.
Decimal numbers are formatted with various combinations
of format strings and IFormatProvider.

IFormatProvider is not used; the default culture is [en-US]:
   No format string:             9876543210.9876543210
   'N' format string:            9,876,543,210.99
   'N5' format string:           9,876,543,210.98765

A CultureInfo object for [nl-NL] is used for the IFormatProvider:
   No format string:             9876543210,9876543210
   'N5' format string:           9.876.543.210,98765

A NumberFormatInfo object with digit group size = 4 and
digit separator = '_' is used for the IFormatProvider:
   'N5' format string:           98_7654_3210,98765
*/

[C++] 
// Sample for the Decimal::ToString( ) methods.
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;
using namespace System::Collections;

void main( )
{
    Decimal nineBillPlus = 
        Convert::ToDecimal( S"9876543210.9876543210" );
        
    Console::WriteLine( S"This example of\n" 
        S"   Decimal::ToString( ), \n" 
        S"   Decimal::ToString( String* ),\n" 
        S"   Decimal::ToString( IFormatProvider* ), and \n" 
        S"   Decimal::ToString( String*, IFormatProvider* )\n" 
        S"generates the following output when run in the " 
        S"[{0}] culture.\nDecimal numbers are formatted " 
        S"with various combinations \nof format strings " 
        S"and IFormatProvider.", 
        CultureInfo::CurrentCulture->Name );

    // Format the number without and with format strings.
    Console::WriteLine( S"\nIFormatProvider is not " 
        S"used; the default culture is [{0}]:", 
        CultureInfo::CurrentCulture->Name );
    Console::WriteLine( S"   {0,-30}{1}", S"No format string:", 
        nineBillPlus.ToString( ) );
    Console::WriteLine( S"   {0,-30}{1}", S"'N' format string:", 
        nineBillPlus.ToString( S"N" ) );
    Console::WriteLine( S"   {0,-30}{1}", S"'N5' format string:", 
        nineBillPlus.ToString( S"N5" ) );
        
    // Create a CultureInfo object for another culture. Use
    // [Dutch - The Netherlands] unless the current culture
    // is Dutch language. In that case use [English - U.S.].
    String* cultureName = 
        CultureInfo::CurrentCulture->Name->Substring( 0, 2 ) == 
        S"nl" ? S"en-US" : S"nl-NL";
    CultureInfo* culture = new CultureInfo( cultureName );
        
    // Use the CultureInfo object for an IFormatProvider.
    Console::WriteLine( S"\nA CultureInfo object " 
        S"for [{0}] is used for the IFormatProvider: ", 
        cultureName );
    Console::WriteLine( S"   {0,-30}{1}", S"No format string:", 
        nineBillPlus.ToString( culture ) );
    Console::WriteLine( S"   {0,-30}{1}", S"'N5' format string:", 
        nineBillPlus.ToString( S"N5", culture ) );
        
    // Get the NumberFormatInfo object from CultureInfo, and
    // then change the digit group size to 4 and the digit
    // separator to '_'.
    NumberFormatInfo* numInfo = culture->NumberFormat;
    Int32 sizes __gc [] = { 4 };
    numInfo->NumberGroupSizes = sizes;
    numInfo->NumberGroupSeparator = S"_";
        
    // Use a NumberFormatInfo object for IFormatProvider.
    Console::WriteLine( 
        S"\nA NumberFormatInfo object with digit group " 
        S"size = 4 and \ndigit separator " 
        S"= '_' is used for the IFormatProvider:" );
    Console::WriteLine( S"   {0,-30}{1}", S"'N5' format string:", 
        nineBillPlus.ToString( S"N5", culture ) );
} 

/*
This example of
   Decimal::ToString( ),
   Decimal::ToString( String* ),
   Decimal::ToString( IFormatProvider* ), and
   Decimal::ToString( String*, IFormatProvider* )
generates the following output when run in the [en-US] culture.
Decimal numbers are formatted with various combinations
of format strings and IFormatProvider.

IFormatProvider is not used; the default culture is [en-US]:
   No format string:             9876543210.9876543210
   'N' format string:            9,876,543,210.99
   'N5' format string:           9,876,543,210.98765

A CultureInfo object for [nl-NL] is used for the IFormatProvider:
   No format string:             9876543210,9876543210
   'N5' format string:           9.876.543.210,98765

A NumberFormatInfo object with digit group size = 4 and
digit separator = '_' is used for the IFormatProvider:
   'N5' format string:           98_7654_3210,98765
*/

[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

Decimal Structure | Decimal Members | System Namespace

Show:
© 2014 Microsoft