Basic String Formatting for Numeric Values

Basic String Formatting for Numeric Values

String formatting is used in many places when you develop with C#. For example, the Console.WriteLine method uses the same string formatting rules as the string type’s Format method. This section provides an introduction to string formatting, using the BattingAverage class as an example.

The format strings passed to methods such as Console.WriteLine control how optional parameters are formatted. When we’ve used Console.WriteLine in earlier examples, we’ve always passed fairly basic strings such as this:

string name = "Mickey";
Console.WriteLine("Hello, {0}.", name);

The {0} notation in this example is used to identify the first parameter, with the number inside the placeholder used to specify the parameter index.

It’s possible to have multiple parameters in a single call to WriteLine, as in the following code:

string name1 = "Mickey";
string name2 = "Ali";
Console.WriteLine("Hello, {0} and {1}.", name1, name2); 

By default, the WriteLine method calls the ToString method for each parameter, substituting the returned string for each placeholder. Alternatively, you can specify width, format, and precision information inside the placeholders. This allows you to apply specific formatting rather than relying on the object’s WriteLine method.

To specify the width used by a parameter, add a comma after the parameter number, followed by the minimum number of characters to be reserved for the parameter, as shown here:

int n = 42;
Console.WriteLine("{0,4},{0,8},{0,12},{0,16}", n);

The format and precision for numeric types can be controlled by using one of eight format characters in the format 0:Sn, where 0 is a number specifying the parameter index, S is a format character, and n is an optional value specifying the precision of the displayed value. Table 4-10 lists the standard .NET formatting characters for numeric types.

The case of the formatting character is insignificant except for X and E, where some portion of the output matches the case of the formatting character.

Alternatively, the formatting string can also include picture information that controls how a number is displayed. In the BattingAverage.ToString method, the string is formatted as follows so that the average will be displayed correctly:

{0:#.000}

This formatting string is passed to the string type’s Format method, which uses the same mechanism employed by Console.WriteLine, as follows:

float average = Average();
string result = string.Format("{0:#.000}", average); 

Passing the initial # as the picture specification will cause a digit to be displayed only if it’s significant. This prevents the display of a leading zero for the batting average, in keeping with the customary format. The next character is a decimal, which causes the decimal separator to be displayed using the proper symbol in the current culture. Finally, the three zeros after the decimal point force exactly three digits to be displayed after the decimal separator.



Part III: Programming Windows Forms