C#中的格式化字元原始碼
阿新 • • 發佈:2019-02-05
一、數字
Specifier | String result | Datatype |
C[n] | $XX,XX.XX | Currency |
($XX,XXX.XX) | ||
D[n] | [-]XXXXXXX | Decimal |
E[n] or e[n] | [-]X.XXXXXXE+xxx | Exponent |
[-]X.XXXXXXe+xxx | ||
[-]X.XXXXXXE-xxx | ||
[-]X.XXXXXXe-xxx | ||
F[n] | [-]XXXXXXX.XX | Fixed point |
G[n] | General or scientific | General |
N[n] | [-]XX,XXX.XX | Number |
X[n] or x[n] | Hex representation |
1、This example uses numeric format specifiers without precision specifiers:
[csharp] view plaincopyprint?- using System;
- class TestDefaultFormats {
- staticvoid Main( ) {
- int i = 654321;
- Console.WriteLine("{0:C}", i); // $654,321.00
- Console.WriteLine("{0:D}", i); // 654321
-
Console.WriteLine("{0:E}"
- Console.WriteLine("{0:F}", i); // 654321.00
- Console.WriteLine("{0:G}", i); // 654321
- Console.WriteLine("{0:N}", i); // 654,321.00
- Console.WriteLine("{0:X}", i); // 9FBF1
- Console.WriteLine("{0:x}", i); // 9fbf1
- }
- }
int
- using System;
- class TestIntegerFormats {
- staticvoid Main( ) {
- int i = 123;
- Console.WriteLine("{0:C6}", i); // $123.000000
- Console.WriteLine("{0:D6}", i); // 000123
- Console.WriteLine("{0:E6}", i); // 1.230000E+002
- Console.WriteLine("{0:G6}", i); // 123
- Console.WriteLine("{0:N6}", i); // 123.000000
- Console.WriteLine("{0:X6}", i); // 00007B
- i = -123;
- Console.WriteLine("{0:C6}", i); // ($123.000000)
- Console.WriteLine("{0:D6}", i); // -000123
- Console.WriteLine("{0:E6}", i); // -1.230000E+002
- Console.WriteLine("{0:G6}", i); // -123
- Console.WriteLine("{0:N6}", i); // -123.000000
- Console.WriteLine("{0:X6}", i); // FFFF85
- i = 0;
- Console.WriteLine("{0:C6}", i); // $0.000000
- Console.WriteLine("{0:D6}", i); // 000000
- Console.WriteLine("{0:E6}", i); // 0.000000E+000
- Console.WriteLine("{0:G6}", i); // 0
- Console.WriteLine("{0:N6}", i); // 0.000000
- Console.WriteLine("{0:X6}", i); // 000000
- }
- }
double
values:
[csharp] view
plaincopyprint?
- using System;
- class TestDoubleFormats {
- staticvoid Main( ) {
- double d = 1.23;
- Console.WriteLine("{0:C6}", d); // $1.230000
- Console.WriteLine("{0:E6}", d); // 1.230000E+000
- Console.WriteLine("{0:G6}", d); // 1.23
- Console.WriteLine("{0:N6}", d); // 1.230000
- d = -1.23;
- Console.WriteLine("{0:C6}", d); // ($1.230000)
- Console.WriteLine("{0:E6}", d); // -1.230000E+000
- Console.WriteLine("{0:G6}", d); // -1.23
- Console.WriteLine("{0:N6}", d); // -1.230000
- d = 0;
- Console.WriteLine("{0:C6}", d); // $0.000000
- Console.WriteLine("{0:E6}", d); // 0.000000E+000
- Console.WriteLine("{0:G6}", d); // 0
- Console.WriteLine("{0:N6}", d); // 0.000000
- }
- }
二、時間 [csharp] view plaincopyprint?
- Console.WriteLine("Standard DateTime Format Specifiers")
- s = String.Format("(d) Short date: . . . . . . . {0:d}" & vbCrLf & _
- "(D) Long date:. . . . . . . . {0:D}" & vbCrLf & _
- "(t) Short time: . . . . . . . {0:t}" & vbCrLf & _
- "(T) Long time:. . . . . . . . {0:T}" & vbCrLf & _
- "(f) Full date/short time: . . {0:f}" & vbCrLf & _
- "(F) Full date/long time:. . . {0:F}" & vbCrLf & _
- "(g) General date/short time:. {0:g}" & vbCrLf & _
- "(G) General date/long time: . {0:G}" & vbCrLf & _
- " (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
- "(M) Month:. . . . . . . . . . {0:M}" & vbCrLf & _
- "(R) RFC1123:. . . . . . . . . {0:R}" & vbCrLf & _
- "(s) Sortable: . . . . . . . . {0:s}" & vbCrLf & _
- "(u) Universal sortable: . . . {0:u} (invariant)" & vbCrLf & _
- "(U) Universal sortable: . . . {0:U}" & vbCrLf & _
- "(Y) Year: . . . . . . . . . . {0:Y}" & vbCrLf, _
- thisDate)