This documentation is archived and is not being maintained.

Formatting Base Types

Use formatting to convert a standard .NET Framework data type to a string that represents that type in some meaningful way. For example, if you have an integer value of 100 that you want to represent as a currency value, you can use the Int32.ToString(String) method and the standard currency format string ("C") to produce a string of "$100.00" on a computer whose current culture is en-US. (Note that computers whose current culture is not en-US will display whatever currency notation is used by the current culture.)

To format a base type, pass the desired format specifier (a string that defines the output format), the desired format provider (an IFormatProvider implementation that identifies the culture whose formatting conventions are used), or both to the ToString method of the object you want to format. If you do not specify a format specifier, or if you pass null (Nothing in Visual Basic), then "G" (the general format) is used as the default. If you do not specify a format provider, if you pass null (Nothing), or if the provider you specify does not provide the required formatting object, the format provider associated with the current thread is used.

In the following example, the ToString(String) method displays the value 100 as a currency-formatted string to the console.

int value = 100;
string currencyValue = value.ToString("C");
// On a system whose current culture is en-US, displays $100.00.