
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
66
|
Chapter 2: Strings and Characters
In addition to the string.Format and the Console.WriteLine methods, the over-
loaded
ToString instance method of a value type may also use the formatting charac-
ters in Table 2-5. Using
ToString, the code looks like this:
float valueAsFloat = 122.35;
string valueAsString = valueAsFloat.ToString("[000000.####]");
After the ToString call, the valueAsString variable holds a formatted version of the
number contained in
valueAsFloat. It looks like this:
[000122.35]
The overloaded ToString method accepts a single parameter of type IFormatProvider.
The
IFormatProvider provided for the valueAsFloat.ToString method is a string con-
taining the formatting for the value type plus any extra text that needs to be supplied.
See Also
See the “String.Format Method,” “Standard Numeric Format Strings,” and “Custom
Numeric Format Strings” topics in the MSDN documentation.
Table 2-5. Custom formatting strings
Formatting character(s) Meaning
0 Use the zero placeholder format. If a digit in the original number exists in this position,
display that digit. If there is no digit in the original string, display a zero.
# Use the digit placeholder format. If a digit in the original number exists in this position,
display that digit. If there is no digit in the original string, display ...