Website development and design blog, tutorials and inspiration

C# String Formatting Examples

How to use the string formatting functions in C#

By , 26th September 2010 in C#

C# like many other programming languages uses special format characters when constructing a string. These characters affect they way in which data is presented on the screen.
 

C and C++ both use sprintf to format a string, you pass in the string with format characters and then the data. The C# equivalent is String.Format.

This method takes as its first parameter a string of text with a list of data type parameters. The format strings, however, are still a little tricky to remember, hence this little guide on c# string formatting.

Format characters are contained within curly braces and are sequentially numbered.

  1. String.Format("This is a number: {0}", 45);

This will output "This is a number: 45", substituting the first data parameter for the {0}. The second data parameter is substituted for {1}, the third for {2} and so on.

These still are not format strings, however, the method simply calls ToString() for each parameter passed in. Format strings could format the 45 into the user's currency (for example £45, $45 or ¥45).

In all these examples the zero in {0:} represents the sequential parameter id, and the value of 12345 is passed in.

Numerical Format Strings

CharacterDescriptionUsageExample Output
cCurrency{0:c}£12,345
dDecimal (whole number){0:d}12345
eScientific{0:e}1.234500e+004
fFixed Point{0:f}12345
gGeneral{0:g}12345
nThousand Separator{0:n}12,345
rRound Triple (decimal only){0:r}System.FormatException
xHexadecimal (int only){0:x4}3039

General Formats

You can also use custom format strings, such as decimal placeholders and lead and trailing characters. In these examples, we pass in 12345.12.

CharacterDescriptionUsageExample Output
0Zero Placeholder{0:00.0000}12345.1200
#Digit Placeholder{0:(#).##}(12345).12
.Decimal Point{0:0.0}12345.12
,Thousand Separator{0:0,0}12,345
%Percent{0:0%}1234512%
eExponent Placeholder{0:00e+0}12e+3

Date/Time Formats

Date/Time formats are dependant on the users locale, so the output may be different.

CharacterDescriptionUsageExample Output
dShort date{0:d}08/12/2007
DLong date{0:D}08 December 2007
tShort time{0:t}15:27
TLong time{0:T}15:27:40
fLong date time{0:f}08 December 2007 15:27
FLong date time{0:F}08 December 2007 15:27:40
gShort date time{0:g}08/12/2007 15:27
GShort date time{0:G}08/12/2007 15:27:40
MShort date{0:M}08 December
rRFC1123 Date time string{0:r}Sat, 08 Dec 2007 15:27:40 GMT
sSortable date/time{0:s}2007-12-08T15:27:40
uUniversal sortable date{0:u}2007-12-08 15:27:40
UUniversal full date{0:U}08 December 2007 15:27:40
YYear month pattern{0:Y}December 2007

Conditional Formats

You can also set conditions for the numerical values using the semi-colon. The syntax is {0:positive;negative;zero}.

Positive, Negative and Zero

  1. // Will output £123
  2. string.Format("{0:£#,##0.00;(£#,##0.00);Zero}", 123);
  3.  
  4. // Will output £(123)
  5. string.Format("{0:£#,##0.00;(£#,##0.00);Zero}", -123);
  6.  
  7. // Will output Zero
  8. string.Format("{0:£#,##0.00;(£#,##0.00);Zero}", 0);

Yes/No

  1. // Output Yes
  2. string.Format("{0:Yes;;No}", 1);
  3.  
  4. // Output No
  5. string.Format("{0:Yes;;No}", 0);
Comments

There are no comments for this post. Be the first!

Leave a Reply

Your email address will not be published.