Veggerby : IBlog | Low decoupling, highly incoherent

Archive for April 2007

Are you (just like me) one of these people who just can’t remember wether A > B should yield 1 or -1 in a compare method. I know there must be some (higher) form of logic behind it that makes it easily deductible when you just think about it.

Anyway as small reference sheet:

Compare(A,B) or A.CompareTo(B)

This should yield:

“Compare statement” Yields Resulting order
A > B 1 B, A
A = B 0 non-deterministic
A < B -1 A, B

Apr/07

15

Formatting strings

After having clicked myself through the MSDN pages of string.Format() one too many times I will, part for my own reference and part for other not wanting to waste valuable synapses memorizing this, compile a list of the common strings when using string.Format().

NB! all results depend more or less on which culture is used, I am for the sake of the majority showing examples in en-us.

Format Name Type Sample value Sample format Sample result
C or c Currency any numerical 12345.6
12345.6
-123.567
{0:C}
{0:C3}
{0:C2}
$12,345.60
$12,345.600
($123.57)
D or d Decimal integer values 12
12
-12
{0:D}
{0:D4}
{0:D4}
12
0012
-0012
D or d   enumeration DayOfWeek.Monday
WD.Fri | WD.Mon
{0:D} 5,
17
d Short date pattern DateTime 21:00:00 2007-02-20 {0:d} 2/20/2007
D Long date pattern DateTime 21:00:00 2007-02-20 {0:D} Tuesday, February 20, 2007
E or e Scientific any numerical 123.5
123.5
123.5
{0:E}
{0:E4}
{0:e4}
1.235000E+002
1.2350E+002
1.2350e+002
F or f Fixed-point any numerical 123.5
123.5
-123.5
{0:F}
{0:F3}
{0:F2}
123.50
123.500
-123.50
F or f   enumeration DayOfWeek.Monday
WD.Fri | WD.Mon
{0:F} Monday
Mon, Fri
f Full date/time pattern (short time) DateTime 21:00:00 2007-02-20 {0:f} Tuesday, February 20, 2007 9:00 PM
F Full date/time pattern (long time) DateTime 21:00:00 2007-02-20 {0:F} Tuesday, February 20, 2007 9:00:00 PM
G or g General floating point 123456.7
123.5
123.5
123.5
123.5
123.5
-123.50
{0:G}
{0:G2}
{0:g2}
{0:G3}
{0:G4}
{0:G5}
{0:G2}
123456.7
1.2E+02
1.2e+02
124
123.5
123.5
-1.2E+02
G or g General integer value 123 (default precision based on type) {0:G}
{0:G1}
{0:g2}
{0:G3}
{0:G4}
123
1E+02
1.2e+02
123
123
G or g   enumeration DayOfWeek.Monday
WD.Fri | WD.Mon
{0:G} Monday
Mon, Fri
g General date/time pattern (short time) DateTime 21:00:00 2007-02-20 {0:g} 2/20/2007 9:00 PM
G General date/time pattern (long time) DateTime 21:00:00 2007-02-20 {0:G} 2/20/2007 9:00:00 PM
M or m Month day pattern DateTime 21:00:00 2007-02-20 {0:M} February 20
N or n Number any numerical 123456.7
12345.6
12345.6
12345.6
-123.45
{0:N}
{0:N0}
{0:N1}
{0:N2}
{0:N1}
123,456.70
12,346
12,345.6
12,345.60
-123.5
o Round-trip date/time pattern DateTime 21:00:00 2007-02-20 {0:o} 2007-02-20T21:00:00.0000000
P or p Percent any numerical 0.123
0.1234
1.234
{0:P}
{0:P1}
{0:P2}
12.30%
12.3%
123.40%
R or r Round-trip single or double 123456789.012 {0:R} 123456789.012
R or r RFC1123 pattern DateTime 21:00:00 2007-02-20 {0:R} Tue, 20 Feb 2007 21:00:00 GMT
s Sortable date/time pattern; conforms to ISO 8601 DateTime 21:00:00 2007-02-20 {0:s} 2007-02-20T21:00:00
t Short time pattern DateTime 21:00:00 2007-02-20 {0:t} 9:00 PM
T Long time pattern DateTime 21:00:00 2007-02-20 {0:T} 9:00:00 PM
u Universal sortable date/time pattern DateTime 21:00:00 2007-02-20 {0:u} 2007-02-20 21:00:00Z
U Universal sortable date/time pattern DateTime 21:00:00 2007-02-20 {0:U} Tuesday, February 20, 2007 8:00:00 PM
Y or y Year month pattern DateTime 21:00:00 2007-02-20 {0:D} February, 2007
X or x Hexadecimal integer value 23712
23712
23712
23712
{0:X}
{0:X2}
{0:X6}
{0:x}
5CA0
5CA0
005CA0
5ca0
X or x   enumeration DayOfWeek.Monday
WD.Fri | WD.Mon
{0:X} 00000005
00000011

Note: When scientific notation comes into play, the case of the format specifier is significant. To have the output with E (capital e), i.e. 1.02E+02 you must you capital format specifier, i.e. {0:E2}. Note that General provides the same result as doing (byte|int|double|...).ToString()

The Round-trip specifier is a bit special. It only allows single or double values and is used to guarantee that a number can be converted back into the same numerical value from it's string representation. It is quite similar to General, and actually tries the General method first and then parses it back into a double or single. If it is not the same result, it extends the default General precision (15 for double and 7 for single) to 17 and 9 respectively. A presicion specifier is ignored for Round-trip.

For the enumeration with FlagsAttribute I have used a custom WD (WeekDay) enumeration like the following:

C#:
  1. [Flags]
  2. public enum WD
  3. {
  4. Mon = 0x1,
  5. Tue = 0x2,
  6. Wed = 0x4,
  7. Thu = 0x8,
  8. Fri = 0x10,
  9. Sat = 0x20,
  10. Sun = 0x40
  11. }

One thing that I often want is to have a number with unknown number of decimals, fx. 1234.567 displayed with thousand separators but without trailing 0 (zeros), i.e. as 1,234.567 not 1,234.58 or 1,234.5670. To accomplish this one can do this (almost perfectly) with custom formatting. If you specify the format string "#,#0.#" (read on this is not the complete truth!!!) it will display all numbers before the decimal separator (specified by "." in the format string) will be thousand separated ("#,#"). The 0 (zero) ensures that 0.12345 is displayed as "0.12345" and not ".12345". The # after the decimal symbol specifies that a decimal should appear (note! a decimal), so "#,#0.#" will display 123456.789 as 123,456.8 (using en-us culture). This is the "almost perfect" part: you'll have to specify the maximum number of decimals to use, so if you use the format string "#,#0.######" you can have a maximum of 6 decimals, but trailing/non-significant zero's are left out.

You can read more about Custom Formatting, the most interesting part is probably the custom date formatting.

Theme Design by devolux.nh2.me