System.Int32.ToString Method

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

Syntax

public string ToString (string format)

Parameters

format
A standard or custom numeric format string.

Returns

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

Exceptions

TypeReason
FormatException format is invalid.

Remarks

The int.ToString(string) method formats an int value in a specified format by using a System.Globalization.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 int.ToString(string) method, as follows:

Default ("G") format

Default (current) culture

int.ToString

Default ("G") format

A specific culture

int.ToString(IFormatProvider)

A specific format

A specific culture

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

The .NET Framework 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 System.Globalization.NumberFormatInfo for the current culture.

Example

This example demonstrates converting a int to a string.

C# Example

using System;
public class Int32ToStringExample {
   public static void Main() {
      Int32 i = 32;
      Console.WriteLine(i);
      String[] formats = {"c", "d", "e", "f", "g", "n", "p", "x" };
      foreach(String str in formats)
         Console.WriteLine("{0}: {1}", str, i.ToString(str));
   }
}

The output is

32
c: $32.00
d: 32
e: 3.200000e+001
f: 32.00
g: 32
n: 32.00
p: 3,200.00 %
x: 20

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0