소프트웨어/C# & ASP.NET

String Formatting

falconer 2009. 8. 13. 13:07

C# like previous C languages uses 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 stings however are still a little tricky to remember, hence this little guide.

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

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 users 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

Character Description Usage Example Output
c Currency {0:c} £12,345
d Decimal (whole number) {0:d} 12345
e Scientific {0:e} 1.234500e+004
f Fixed Point {0:f} 12345
g General {0:g} 12345
n Thousand Separator {0:n} 12,345
r Round Triple (decimal only) {0:r} System.FormatException
x Hexadecimal (int only) {0:x4} 3039

 

General Formats

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

Character Description Usage Example Output
0 Zero 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%
e Exponent Placeholder {0:00e+0} 12e+3

 

Date/Time Formats

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

Character Description Usage Example Output
d {0:d} 08/12/2007
D {0:D} 08 December 2007
t {0:t} 15:27
T {0:T} 15:27:40
f {0:f} 08 December 2007 15:27
F {0:F} 08 December 2007 15:27:40
g {0:g} 08/12/2007 15:27
G {0:G} 08/12/2007 15:27:40
M {0:M} 08 December
r {0:r} Sat, 08 Dec 2007 15:27:40 GMT
s {0:s} 2007-12-08T15:27:40
u {0:u} 2007-12-08 15:27:40
U {0:U} 08 December 2007 15:27:40
Y {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

// Will output £123
tring.Format("{0:£#,##0.00;(£#,##0.00);Zero}", 123);

// Will output £(123)
String.Format("{0:£#,##0.00;(£#,##0.00);Zero}", -123);

// Will output Zero
String.Format("{0:£#,##0.00;(£#,##0.00);Zero}", 0);

 

Yes/No

// Output Yes
String.Format("{0:Yes;;No}", 1);

// Output No
String.Format("{0:Yes;;No}", 0);

 출처 : 

참고 : http://gaedong2.tistory.com/236