Decimal.ToString Method (String, IFormatProvider)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Function ToString ( _
    format As String, _
    provider As IFormatProvider _
) As String
[SecuritySafeCriticalAttribute]
public string ToString(
    string format,
    IFormatProvider provider
)

Parameters

  • format
    Type: System.String
    A standard or custom numeric format string (see Remarks).

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)

Exceptions

Exception Condition
FormatException

format is invalid.

Remarks

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 nulla null reference (Nothing in Visual Basic) 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:

NoteNote:

Because the Decimal data type is not supported on the Macintosh OS X operating system, the string representation of a Decimal value may be different from those of the other .NET Framework numeric types that are supported by OS X.

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 nulla null reference (Nothing in Visual Basic), the thread current culture is used.

Examples

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

Dim value As Decimal = 16325.62D
Dim specifier As String
Dim culture As CultureInfo

' Use standard numeric format specifiers.
specifier = "G"
culture = New CultureInfo("eu-ES")
outputBlock.Text &= String.Format(value.ToString(specifier, culture)) & vbCrLf
' Displays:    16325,62
outputBlock.Text &= String.Format(value.ToString(specifier, CultureInfo.InvariantCulture)) & vbCrLf
' Displays:    16325.62

specifier = "C"
culture = New CultureInfo("en-US")
outputBlock.Text &= String.Format(value.ToString(specifier, culture)) & vbCrLf
' Displays:    $16,325.62
culture = New CultureInfo("en-GB")
outputBlock.Text &= String.Format(value.ToString(specifier, culture)) & vbCrLf
' Displays:    £16,325.62

specifier = "E04"
culture = New CultureInfo("sv-SE")
outputBlock.Text &= String.Format(value.ToString(specifier, culture)) & vbCrLf
' Displays: 1,6326E+004   
culture = New CultureInfo("en-NZ")
outputBlock.Text &= String.Format(value.ToString(specifier, culture)) & vbCrLf
' Displays:    1.6326E+004   

specifier = "F"
culture = New CultureInfo("fr-FR")
outputBlock.Text &= String.Format(value.ToString(specifier, culture)) & vbCrLf
' Displays:    16325,62
culture = New CultureInfo("en-CA")
outputBlock.Text &= String.Format(value.ToString(specifier, culture)) & vbCrLf
' Displays:    16325.62

specifier = "N"
culture = New CultureInfo("es-ES")
outputBlock.Text &= String.Format(value.ToString(specifier, culture)) & vbCrLf
' Displays:    16.325,62
culture = New CultureInfo("fr-CA")
outputBlock.Text &= String.Format(value.ToString(specifier, culture)) & vbCrLf
' Displays:    16 325,62

specifier = "P"
culture = CultureInfo.InvariantCulture
outputBlock.Text &= String.Format((value / 10000).ToString(specifier, culture)) & vbCrLf
' Displays:    163.26 %
culture = New CultureInfo("ar-EG")
outputBlock.Text &= String.Format((value / 10000).ToString(specifier, culture)) & vbCrLf
' Displays:    163.256 %
decimal value = 16325.62m;
string specifier;
CultureInfo culture;

// Use standard numeric format specifiers.
specifier = "G";
culture = new CultureInfo("eu-ES");
outputBlock.Text += String.Format(value.ToString(specifier, culture)) + "\n";
// Displays:    16325,62
outputBlock.Text += String.Format(value.ToString(specifier, CultureInfo.InvariantCulture)) + "\n";
// Displays:    16325.62

specifier = "C";
culture = new CultureInfo("en-US");
outputBlock.Text += String.Format(value.ToString(specifier, culture)) + "\n";
// Displays:    $16,325.62
culture = new CultureInfo("en-GB");
outputBlock.Text += String.Format(value.ToString(specifier, culture)) + "\n";
// Displays:    £16,325.62

specifier = "E04";
culture = new CultureInfo("sv-SE");
outputBlock.Text += String.Format(value.ToString(specifier, culture)) + "\n";
// Displays: 1,6326E+004   
culture = new CultureInfo("en-NZ");
outputBlock.Text += String.Format(value.ToString(specifier, culture)) + "\n";
// Displays:    1.6326E+004   

specifier = "F";
culture = new CultureInfo("fr-FR");
outputBlock.Text += String.Format(value.ToString(specifier, culture)) + "\n";
// Displays:    16325,62
culture = new CultureInfo("en-CA");
outputBlock.Text += String.Format(value.ToString(specifier, culture)) + "\n";
// Displays:    16325.62

specifier = "N";
culture = new CultureInfo("es-ES");
outputBlock.Text += String.Format(value.ToString(specifier, culture)) + "\n";
// Displays:    16.325,62
culture = new CultureInfo("fr-CA");
outputBlock.Text += String.Format(value.ToString(specifier, culture)) + "\n";
// Displays:    16 325,62

specifier = "P";
culture = CultureInfo.InvariantCulture;
outputBlock.Text += String.Format((value / 10000).ToString(specifier, culture)) + "\n";
// Displays:    163.26 %
culture = new CultureInfo("ar-EG");
outputBlock.Text += String.Format((value / 10000).ToString(specifier, culture)) + "\n";
// Displays:    163.256 %

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.