Olvasás angol nyelven Szerkesztés

Megosztás a következőn keresztül:


Decimal.ToString Method

Definition

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

Overloads

ToString()

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

ToString(IFormatProvider)

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

ToString(String)

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

ToString(String, IFormatProvider)

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

ToString()

Source:
Decimal.cs
Source:
Decimal.cs
Source:
Decimal.cs

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

public override string ToString ();

Returns

A string that represents the value of this instance.

Examples

The following example displays a Decimal value using the default ToString() method. It also displays the string representations of the Decimal value that result from using a number of standard format specifiers.

decimal value = -16325.62m;
// Display value using default ToString method.
Console.WriteLine(value.ToString());            // Displays -16325.62
// Display value using some standard format specifiers.
Console.WriteLine(value.ToString("G"));         // Displays -16325.62
Console.WriteLine(value.ToString("C"));         // Displays ($16,325.62)
Console.WriteLine(value.ToString("F"));         // Displays -16325.62

The following example displays the amount of money in an account.

class PiggyBank {
    public void AddPenny() {
        MyFortune = Decimal.Add(MyFortune, .01m);
    }

    public override string ToString() {
        return MyFortune.ToString("C")+" in piggy bank";
    }

    protected decimal MyFortune;
}

Remarks

The ToString() method formats a Decimal value in the default ("G", or general) format of the current culture. If you want to specify a different format or culture, use the other overloads of the ToString method, as follows:

To use format For culture Use the overload
Default ("G") format A specific culture ToString(IFormatProvider)
A specific format Default (current) culture ToString(String)
A specific format A specific culture ToString(String, IFormatProvider)

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

See also

Applies to

.NET 9 és más verziók
Termék Verziók
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToString(IFormatProvider)

Source:
Decimal.cs
Source:
Decimal.cs
Source:
Decimal.cs

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

public string ToString (IFormatProvider provider);
public string ToString (IFormatProvider? provider);

Parameters

provider
IFormatProvider

An object that supplies culture-specific formatting information.

Returns

The string representation of the value of this instance as specified by provider.

Implements

Examples

The following example displays the string representation of a Decimal value using CultureInfo objects that represent several different cultures.

decimal value = -16325.62m;
// Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture));
// Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")));
// Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")));
// This example displays the following output to the console:
//       -16325.62
//       -16325.62
//       -16325,62

Remarks

The ToString(IFormatProvider) method formats a Decimal value in the default ("G", or general) format of a specified culture. If you want to specify a different format or the current culture, use the other overloads of the ToString method, as follows:

To use format For culture Use the overload
Default ("G") format Default (current) culture ToString()
A specific format Default (current) culture ToString(String)
A specific format A specific culture ToString(String, IFormatProvider)

.NET 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 object or a CultureInfo object. The provider parameter supplies culture-specific information used in formatting. If provider is null, the thread current culture is used.

To convert a Decimal value to its string representation using a specified culture and a specific format string, call the Decimal.ToString(String, IFormatProvider) method.

See also

Applies to

.NET 9 és más verziók
Termék Verziók
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToString(String)

Source:
Decimal.cs
Source:
Decimal.cs
Source:
Decimal.cs

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

public string ToString (string format);
public string ToString (string? format);

Parameters

format
String

A standard or custom numeric format string.

Returns

The string representation of the value of this instance as specified by format.

Exceptions

format is invalid.

Examples

The following example displays a Decimal value using each of the supported standard numeric format specifiers, together with two custom numeric format strings. In converting the numeric values to strings, the example uses the formatting conventions of the en-US culture.

decimal value = 16325.62m;
string specifier;

// Use standard numeric format specifiers.
specifier = "G";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    G: 16325.62
specifier = "C";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    C: $16,325.62
specifier = "E04";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    E04: 1.6326E+004
specifier = "F";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    F: 16325.62
specifier = "N";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    N: 16,325.62
specifier = "P";
Console.WriteLine("{0}: {1}", specifier, (value/10000).ToString(specifier));
// Displays:    P: 163.26 %

// Use custom numeric format specifiers.
specifier = "0,0.000";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    0,0.000: 16,325.620
specifier = "#,#.00#;(#,#.00#)";
Console.WriteLine("{0}: {1}", specifier, (value*-1).ToString(specifier));
// Displays:    #,#.00#;(#,#.00#): (16,325.62)

Remarks

The ToString(String) method formats a Decimal value in a specified format by using the conventions of the current culture. If you want to use the default ("G", or general) format or specify a different culture, use the other overloads of the ToString method, as follows:

To use format For culture Use the overload
Default ("G") format Default (current) culture ToString()
Default ("G") format A specific culture ToString(IFormatProvider)
A specific format A specific culture ToString(String, IFormatProvider)

The ToString method uses the standard or custom numeric format string specified by the format parameter to convert the value of the current instance into its string representation. 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 of this instance is formatted with the general numeric format specifier (G).

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

The return value is formatted by using the NumberFormatInfo object for the current culture. To apply the formatting conventions of a specified culture, call the ToString(String, IFormatProvider) method.

See also

Applies to

.NET 9 és más verziók
Termék Verziók
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToString(String, IFormatProvider)

Source:
Decimal.cs
Source:
Decimal.cs
Source:
Decimal.cs

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

public string ToString (string format, IFormatProvider provider);
public string ToString (string? format, IFormatProvider? provider);

Parameters

format
String

A numeric format string.

provider
IFormatProvider

An object that supplies culture-specific formatting information.

Returns

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

Implements

Exceptions

format is invalid.

Examples

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 %

Remarks

The ToString(String, IFormatProvider) method formats a Decimal value in a specified format of a specified culture. If you want to use default format or culture settings, use the other overloads of the ToString method, as follows:

To use format For culture Use the overload
Default ("G") format Default (current) culture ToString()
Default ("G") format A specific culture ToString(IFormatProvider)
A specific format Default (current) culture ToString(String)

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").

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

See also

Applies to

.NET 9 és más verziók
Termék Verziók
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0