Compartir a través de


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 ToString method and the currency format string ("C") to produce a string of "$100.00". Note that computers that do not have U.S. English specified as the current culture will display whatever currency notation is used by the current culture.

To format a base type, pass the desired format specifier, the desired format provider, 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 have the property pertaining to the requested action, the format provider associated with the current thread is used.

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

Dim MyInt As Integer = 100
Dim MyString As String = MyInt.ToString("C")
Console.WriteLine(MyString)
[C#]
int MyInt = 100;
String MyString = MyInt.ToString("C");
Console.WriteLine(MyString);

See Also

Formatting Types