(How)
C^# You Are - 6 May 2007
This week's questions are all about formatting values as strings.
- How do you specify a precision when formatting a float value as a string? Answer
The numeric precision is specified as a number after the format specifier.
Depending on the specifier used the value is truncated, rounded or left unchanged
if the precision is less than the actual value.
int value = 123;
Console.WriteLine(String.Format("{0:d4}", value)); //Prints 0123
Console.WriteLine(String.Format("{0:d2}", value)); //Prints 123
Console.WriteLine(String.Format("{0:f4}", value)); //Prints 0123.0000
Console.WriteLine(String.Format("{0:f2}",
value)); //Prints 123.00
Console.WriteLine(String.Format("{0:g4}", value)); //Prints 123
Console.WriteLine(String.Format("{0:g2}", value)); //Prints 12.e+02
- How do you add thousands separators to an integral value when formatting as a string? Answer
The general number (n) specifier is used to add thousands separators
to the number.
int
value = 12345678;
Console.WriteLine(String.Format("{0:n}", value)); //Prints: 12,345,678.00
Console.WriteLine(String.Format("{0:n0}", value)); //Prints: 12,345,678
- How do you ensure that a number is at least 3 digits in size (using zeroes to fill
in the number) when formatting as a string? Answer
The d3 specifier will ensure that the number is at least 3 digits
in size and add zeros to ensure that it is accurate. An alternative is to
use the 0 specifier to specify the exact zeroes to use.
int value = 123;
Console.WriteLine(String.Format("{0:000}", value)); //Prints: 123
Console.WriteLine(String.Format("{0:0000}", value)); //Prints: 0123
- How do you make a date/time value format itself using the local time format settings? Answer
The date/time will automatically format itself using the local time format settings.
However if the time is in UTC then the UTC value will be shown in the local time
format but it will still represent the UTC time value.
Console.WriteLine(String.Format("{0}", DateTime.Now)); //Prints:
May 6, 2007 4:32:00 PM
Console.WriteLine(String.Format("{0}", DateTime.UtcNow)); //Prints: May 6,
2007 8:32:00 PM
- How do you make a date/time value format itself in a manner that it can be round-tripped
back to the original value? Answer
Most date/time formats can be round-tripped back from a string due to the flexibility
of the parser. However to truly ensure the value gets translated properly
you should use a culture-neutral format like R which uses a standards-based
format. Technically the format uses a culture invariant format but it should
still translate properly.
Console.WriteLine(String.Format("{0:R}", DateTime.Now)); //Prints: Sun, 06
May 2007 16:32:00 GMT
For completeness, use the R specifier for other numeric values.
- How do you display the milliseconds of a date/time value when formatting it as a
string? Answer
Technically you can't (at least based on the documentation). Instead the documentation
refers to fractional seconds which just happen to line up with the milliseconds.
Use the f specifier to indicate a digit of the milliseconds.
You can specify up to 7 of them.
DateTime dt = DateTime.Now; //Milliseconds = 906
Console.WriteLine(String.Format("{0}, {1:fffffff}", dt.Millisecond,dt)); //Prints:
906, 9062500
- How do you format a float value as a monetary value when converting to a string? Answer
Currency is identified using the Cor c specifiers.
It uses the current locale settings to format the currency value.
float cost = 3.57F;
Console.WriteLine(String.Format("{0:c}", cost)); //Prints $3.57
- How do you display an integral value in hexadecimal when formatting as a string? Answer
Use the X for upper-case hex digits or x for lower-case
digits.
int value = 123456;
Console.WriteLine(String.Format("{0:X}", value)); //Prints: 1E240
Console.WriteLine(String.Format("0x{0:X}", value)); //Prints: 0x1E240
Ironically .NET does not support any additional base specifiers such as binary or octal.
- How do you right-justify an integral value to 10 characters? Answer
Use the alignment specifier to specify a value of 10. The alignment specifier
follows the index of the specifier but before the format specifier and preceded
by a comma.
int value = 123456;
Console.WriteLine(String.Format("{0,4}", value)); //Prints: 123456
Console.WriteLine(String.Format("{0,9}", value)); //Prints:
123456
- How do you format an integral value as a normal number but in parenthesis when it is negative? Answer
Formatting supports three different sections of specifiers (separated by semicolons).
The first section represents positive numbers, the second negative numbers and the
third zero. Therefore you can use different formatting for each of the three
possible values.
int value = 123;
int value2 = -123;
Console.WriteLine(value.ToString("###;(###);Zero")); //Prints: 123
Console.WriteLine(value2.ToString("#;(###);Zero")); //Prints: (123)
Notice that in the last example is used the ToString method instead
of String.Format. String.Format does not
support (from what I can tell) multiple sections. Instead it will simply print
out the text and move on.
|
|