日期:2009-01-23  浏览次数:20552 次

Every .NET Framework base data type (Int32, Int64, Single, Double, and so on) contains a Format method and a ToString method that return a string-formatted representation of the original value. The Format method provides several formatting options and the ability to dynamically change output based on the current culture. The ToString method provides only one formatting option and is not influenced by the current culture.

The Format method is useful if you want to convert one of the standard .NET Framework data types to some other format. For example, if you have an integer value of 100 that you want to represent to the user as a currency value, you could easily use the Format method to produce a string of "$100.00". Note that the original value contained in the data type is not converted, but a new string is created that represents the original value. This new string cannot be used for calculation until it is converted back to a .NET base data type. The original value, however, can continue to be calculated at anytime.

The Format method takes the following forms, where XXX is the name of the numeric base data-type:

  static XXX Format(XXX value, String format);
  static XXX Format(XXX value, String format,
                               NumberFormatInfo info);
  static XXX Format(String format,
                IServiceObjectProvider s);
The Format method takes a combination of three parameters: a value, a string format specifier, and an IServiceObjectProvider interface.

The IServiceObjectProvider flag is used to change the culture. It specifies characters to use for decimal and thousand separators and the spelling and placement of currency symbols. When set to null (in Visual Basic Nothing), it will use the characters specified by the current culture.

Two format strings are accepted by the Format method: standard numeric format strings and picture numeric format strings. Standard format strings are built-in options that address the most common formatting requirements, while picture strings are constructed by developers to address additional formatting requirements.

In following examples the Format method displays the value of 100 as a currency-formatted string in the console's output window:

[C#]
int MyInt = 100;
MyInt.Format("C", null);
//Returns "$100.00"
In this example, the Format method takes two parameters: the format string and IServiceObjectProvider. The format string specifies the return value of the Format method. In this case, the string has a value of "C", which specifies that the Format method will return a currency representation of the original value. You can safely set the IServiceObjectProvider flag to null (Nothing), specifying that the default current culture will be used. Note that the Format method returns a string data type, not a numeric data type.

The following example is similar to the previous example, except that the value is not retrieved from a previously declared int variable:

[C#]
int.Format( 100, "C");
//Returns "$100.00"
In this example, the value to be formatted is passed as the first parameter and the format string is passed as the second. The .NET Framework allows you to format any value without actually declaring and initializing it as a variable if you use the above syntax.

The ToString method quickly converts a .NET Framework base type value into a string.