Export (0) Print
Expand All

Decimal.ToString Method (String, IFormatProvider)

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

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

public string ToString (
	string format,
	IFormatProvider provider
)
public final String ToString (
	String format, 
	IFormatProvider provider
)
public final function ToString (
	format : String, 
	provider : IFormatProvider
) : String

Parameters

format

A format specification.

provider

An IFormatProvider that supplies culture-specific formatting information.

Return Value

The String representation of the value of this instance as specified by format and provider.

Exception typeCondition

FormatException

format is invalid.

If format is a null reference (Nothing in Visual Basic) or an empty string, the return value for this instance is formatted with the general format specifier ("G").

Parameter provider is an IFormatProvider that obtains a NumberFormatInfo. The NumberFormatInfo provides culture-specific format information about this instance. If provider is a null reference (Nothing in Visual Basic), the return value for this instance is formatted with the NumberFormatInfo for the current culture.

The following code example formats a Decimal number with several overloads of the ToString method.

// 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
*/

// Example for the Decimal.ToString( ) methods.
import System.*;
import System.Globalization.*;
import Microsoft.VisualBasic.*;

class DecimalToStringDemo
{
    public static void main(String[] args)
    {
        System.Decimal nineBillPlus = 
            System.Convert.ToDecimal(9876543210.9876543210);
      
        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.get_CurrentCulture().get_Name());
      
        // Format the number without and with format strings.
        Console.WriteLine("\nIFormatProvider is not " 
            + "used; the default culture is [{0}]:", 
            CultureInfo.get_CurrentCulture().get_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.get_CurrentCulture().get_Name().
            Substring(0, 2).Equals("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.get_NumberFormat();
        numInfo.set_NumberGroupSizes(new int[]{4});
        numInfo.set_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.98765
   '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,98765
   '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
 */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft