Edit

Share via


Int32.ToString Method

Definition

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

Overloads

ToString(IFormatProvider)

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

ToString()

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

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(IFormatProvider)

Source:
Int32.cs
Source:
Int32.cs
Source:
Int32.cs

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

C#
public string ToString(IFormatProvider provider);
C#
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 an Int32 value using CultureInfo objects that represent several different cultures.

C#
int value = -16325;
// 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
//       -16325
//       -16325

Remarks

The ToString(IFormatProvider) method formats an Int32 value in the default ("G", or general) format by using the NumberFormatInfo object 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)

The provider parameter is an object that implements the IFormatProvider interface. Its GetFormat method returns a NumberFormatInfo object that provides culture-specific information about the format of the string that is returned by this method. The object that implements IFormatProvider can be any of the following:

If provider is null or a NumberFormatInfo object cannot be obtained from provider, the return value is formatted using the NumberFormatInfo object for the thread current culture. For information about the thread current culture, see Thread.CurrentCulture.

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

See also

Applies to

.NET 10 and other versions
Product Versions
.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, 10
.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()

Source:
Int32.cs
Source:
Int32.cs
Source:
Int32.cs

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

C#
public override string ToString();

Returns

The string representation of the value of this instance, consisting of a negative sign if the value is negative, and a sequence of digits ranging from 0 to 9 with no leading zeroes.

Examples

The following example displays an Int32 value using the default ToString() method. It also displays the string representations of the Int32 value that results from using a number of standard format specifiers. The examples are displayed using the formatting conventions of the en-US culture.

C#
int value = -16325;
// Display value using default ToString method.
Console.WriteLine(value.ToString());            // Displays -16325
// Display value using some standard format specifiers.
Console.WriteLine(value.ToString("G"));         // Displays -16325
Console.WriteLine(value.ToString("C"));         // Displays ($16,325.00)
Console.WriteLine(value.ToString("D"));         // Displays -16325
Console.WriteLine(value.ToString("F"));         // Displays -16325.00
Console.WriteLine(value.ToString("N"));         // Displays -16,325.00
Console.WriteLine(value.ToString("X"));         // Displays FFFFC03B

Remarks

The ToString() method formats an Int32 value in the default ("G", or general) format by using the NumberFormatInfo object 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 10 and other versions
Product Versions
.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, 10
.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:
Int32.cs
Source:
Int32.cs
Source:
Int32.cs

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

C#
public string ToString(string format);
C#
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 or not supported.

Examples

The following example displays an Int32 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.

C#
int value = -16325;
string specifier;

// Use standard numeric format specifier.
specifier = "G";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    G: -16325
specifier = "C";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    C: ($16,325.00)
specifier = "D8";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    D8: -00016325
specifier = "E4";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    E4: -1.6325E+004
specifier = "e3";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    e3: -1.633e+004
specifier = "F";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    F: -16325.00
specifier = "N";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    N: -16,325.00
specifier = "P";
Console.WriteLine("{0}: {1}", specifier, (value/100000).ToString(specifier));
// Displays:    P: -16.33 %
specifier = "X";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    X: FFFFC03B

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

Remarks

The ToString(String) method formats an Int32 value in a specified format by using a NumberFormatInfo object that represents 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 format parameter can be any valid standard numeric format specifier except for "R", 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 of this instance is formatted with the NumberFormatInfo for the current culture.

See also

Applies to

.NET 10 and other versions
Product Versions
.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, 10
.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:
Int32.cs
Source:
Int32.cs
Source:
Int32.cs

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

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

Parameters

format
String

A standard or custom 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 or not supported.

Examples

The following example displays a positive and a negative value using each of the supported standard numeric format specifiers for three different cultures.

C#
// Define cultures whose formatting conventions are to be used.
CultureInfo[] cultures = {CultureInfo.CreateSpecificCulture("en-US"),
                          CultureInfo.CreateSpecificCulture("fr-FR"),
                          CultureInfo.CreateSpecificCulture("es-ES") };
int positiveNumber = 1679;
int negativeNumber = -3045;
string[] specifiers = {"G", "C", "D8", "E2", "F", "N", "P", "X8"};

foreach (string specifier in specifiers)
{
   foreach (CultureInfo culture in cultures)
   {
      // Display values with "G" format specifier.
      Console.WriteLine("{0} format using {1} culture: {2, 16} {3, 16}",
                        specifier, culture.Name,
                        positiveNumber.ToString(specifier, culture),
                        negativeNumber.ToString(specifier, culture));
   }
   Console.WriteLine();
}
// The example displays the following output:
//       G format using en-US culture:             1679            -3045
//       G format using fr-FR culture:             1679            -3045
//       G format using es-ES culture:             1679            -3045
//
//       C format using en-US culture:        $1,679.00      ($3,045.00)
//       C format using fr-FR culture:       1 679,00 €      -3 045,00 €
//       C format using es-ES culture:       1.679,00 €      -3.045,00 €
//
//       D8 format using en-US culture:         00001679        -00003045
//       D8 format using fr-FR culture:         00001679        -00003045
//       D8 format using es-ES culture:         00001679        -00003045
//
//       E2 format using en-US culture:        1.68E+003       -3.05E+003
//       E2 format using fr-FR culture:        1,68E+003       -3,05E+003
//       E2 format using es-ES culture:        1,68E+003       -3,05E+003
//
//       F format using en-US culture:          1679.00         -3045.00
//       F format using fr-FR culture:          1679,00         -3045,00
//       F format using es-ES culture:          1679,00         -3045,00
//
//       N format using en-US culture:         1,679.00        -3,045.00
//       N format using fr-FR culture:         1 679,00        -3 045,00
//       N format using es-ES culture:         1.679,00        -3.045,00
//
//       P format using en-US culture:     167,900.00 %    -304,500.00 %
//       P format using fr-FR culture:     167 900,00 %    -304 500,00 %
//       P format using es-ES culture:     167.900,00 %    -304.500,00 %
//
//       X8 format using en-US culture:         0000068F         FFFFF41B
//       X8 format using fr-FR culture:         0000068F         FFFFF41B
//       X8 format using es-ES culture:         0000068F         FFFFF41B

Remarks

The ToString(String, IFormatProvider) method formats an Int32 value in a specified format by using the NumberFormatInfo object 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 either a standard or a custom numeric format string. All standard numeric format strings other than "R" (or "r") are supported, as are all custom numeric format characters. If format is null or an empty string (""), the return value for this instance is formatted with the general numeric format specifier ("G").

The provider parameter is an object that implements the IFormatProvider interface. Its GetFormat method returns a NumberFormatInfo object that provides culture-specific format information about the format of the string that is returned by this method. The object that implements IFormatProvider can be any of the following:

If provider is null or a NumberFormatInfo object cannot be obtained from provider, the return value for this instance is formatted with the NumberFormatInfo for the current culture.

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

See also

Applies to

.NET 10 and other versions
Product Versions
.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, 10
.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