int 예제
Add zeroes before number
String.Format("{0:00000}", 15);
String.Format("{0:00000}", -15);
Align number to the right or left
String.Format("{0,5}", 15);
String.Format("{0,-5}", 15);
String.Format("{0,5:000}", 15);
String.Format("{0,-5:000}", 15);
Different formatting for negative numbers and zero
String.Format("{0:#;minus #}", 15);
String.Format("{0:#;minus #}", -15);
String.Format("{0:#;minus #;zero}", 0);
Custom number formatting (e.g. phone number)
String.Format("{0:+### ### ### ###}", 447900123456);
String.Format("{0:##-####-####}", 8958712551);
double 예제
Digits after decimal point
String.Format("{0:0.00}", 123.4567);
String.Format("{0:0.00}", 123.4);
String.Format("{0:0.00}", 123.0);
String.Format("{0:0.##}", 123.4567);
String.Format("{0:0.##}", 123.4);
String.Format("{0:0.##}", 123.0);
Digits before decimal point
String.Format("{0:00.0}", 123.4567);
String.Format("{0:00.0}", 23.4567);
String.Format("{0:00.0}", 3.4567);
String.Format("{0:00.0}", -3.4567);
Thousands separator
String.Format("{0:0,0.0}", 12345.67);
String.Format("{0:0,0}", 12345.67);
Zero
String.Format("{0:0.0}", 0.0);
String.Format("{0:0.#}", 0.0);
String.Format("{0:#.0}", 0.0);
String.Format("{0:#.#}", 0.0);
Align numbers with spaces
String.Format("{0,10:0.0}", 123.4567);
String.Format("{0,-10:0.0}", 123.4567);
String.Format("{0,10:0.0}", -123.4567);
String.Format("{0,-10:0.0}", -123.4567);
Custom formatting for negative numbers and zero
String.Format("{0:0.00;minus 0.00;zero}", 123.4567);
String.Format("{0:0.00;minus 0.00;zero}", -123.4567);
String.Format("{0:0.00;minus 0.00;zero}", 0.0);
Some funny examples
String.Format("{0:my number is 0.0}", 12.3);
String.Format("{0:0aaa.bbb0}", 12.3);
DateTime 예제
Custom DateTime Formatting
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);
String.Format("{0:y yy yyy yyyy}", dt);
String.Format("{0:M MM MMM MMMM}", dt);
String.Format("{0:d dd ddd dddd}", dt);
String.Format("{0:h hh H HH}", dt);
String.Format("{0:m mm}", dt);
String.Format("{0:s ss}", dt);
String.Format("{0:f ff fff ffff}", dt);
String.Format("{0:F FF FFF FFFF}", dt);
String.Format("{0:t tt}", dt);
String.Format("{0:z zz zzz}", dt);
String.Format("{0:d/M/yyyy HH:mm:ss}", dt);
String.Format("{0:d/M/yyyy HH:mm:ss}", dt);
String.Format("{0:M/d/yyyy}", dt);
String.Format("{0:MM/dd/yyyy}", dt);
String.Format("{0:ddd, MMM d, yyyy}", dt);
String.Format("{0:dddd, MMMM d, yyyy}", dt);
String.Format("{0:MM/dd/yy}", dt);
String.Format("{0:MM/dd/yyyy}", dt);