Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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
)

Parameters

format
Type: System.String
A numeric format string (see Remarks).
provider
Type: System.IFormatProvider
An object that supplies culture-specific formatting information.

Return Value

Type: System.String
The string representation of the value of this instance as specified by format and provider.

Implements

IFormattable.ToString(String, IFormatProvider)
ExceptionCondition
FormatException

format is invalid.

The format parameter can be any valid standard numeric format specifier except for D, R, and X, as well as any combination of custom numeric format specifiers. If format is null or an empty string, the return value for this instance is formatted with the general numeric format specifier ("G").

The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:

The provider parameter is an IFormatProvider implementation whose IFormatProvider.GetFormat method returns a NumberFormatInfo object. Typically, provider is a NumberFormatInfo or CultureInfo object. The provider parameter supplies culture-specific information used in formatting. If provider is null, the thread current culture is used.

The following example displays a Decimal value using each of the supported standard numeric format specifiers for several different cultures.


decimal value = 16325.62m;
string specifier;
CultureInfo culture;

// Use standard numeric format specifiers.
specifier = "G";
culture = CultureInfo.CreateSpecificCulture("eu-ES");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16325,62
Console.WriteLine(value.ToString(specifier, CultureInfo.InvariantCulture));
// Displays:    16325.62

specifier = "C";
culture = CultureInfo.CreateSpecificCulture("en-US");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    $16,325.62
culture = CultureInfo.CreateSpecificCulture("en-GB");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    £16,325.62

specifier = "E04";
culture = CultureInfo.CreateSpecificCulture("sv-SE");
Console.WriteLine(value.ToString(specifier, culture));
// Displays: 1,6326E+004   
 culture = CultureInfo.CreateSpecificCulture("en-NZ");
 Console.WriteLine(value.ToString(specifier, culture));
// Displays:    1.6326E+004   

specifier = "F";
culture = CultureInfo.CreateSpecificCulture("fr-FR");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16325,62
culture = CultureInfo.CreateSpecificCulture("en-CA");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16325.62

specifier = "N";
culture = CultureInfo.CreateSpecificCulture("es-ES");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16.325,62
culture = CultureInfo.CreateSpecificCulture("fr-CA");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16 325,62

specifier = "P";
culture = CultureInfo.InvariantCulture;
Console.WriteLine((value/10000).ToString(specifier, culture));
// Displays:    163.26 %
culture = CultureInfo.CreateSpecificCulture("ar-EG");
Console.WriteLine((value/10000).ToString(specifier, culture));
// Displays:    163.256 %


.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.