示例代码:
class Program
{
enumColor {Yellow = 1, Blue, Green};
staticDateTime thisDate =DateTime.Now;
publicstatic void Main()
{
//Store the output of the String.Format method in a string.
strings = "";
Console.Clear();
//Format a negative integer or floating-point number in variousways.
Console.WriteLine("StandardNumeric Format Specifiers");
s= String.Format(
"(C)Currency: . . . . . . . . {0:C}\n" +
"(D)Decimal:. . . . . . . . . {0:D}\n" +
"(E)Scientific: . . . . . . . {1:E}\n" +
"(F)Fixed point:. . . . . . . {1:F}\n" +
"(G)General:. . . . . . . . . {0:G}\n" +
" (default):.. . . . . . . {0} (default = 'G')\n" +
"(N)Number: . . . . . . . . . {0:N}\n" +
"(P)Percent:. . . . . . . . . {1:P}\n" +
"(R)Round-trip: . . . . . . . {1:R}\n" +
"(X)Hexadecimal:. . . . . . . {0:X}\n",
-123,-123.45f);
Console.WriteLine(s);
// Format the current date in various ways.
Console.WriteLine("StandardDateTime Format Specifiers");
s= String.Format(
"(d)Short date: . . . . . . . {0:d}\n" +
"(D)Long date:. . . . . . . . {0:D}\n" +
"(t)Short time: . . . . . . . {0:t}\n" +
"(T)Long time:. . . . . . . . {0:T}\n" +
"(f)Full date/short time: . . {0:f}\n" +
"(F)Full date/long time:. . . {0:F}\n" +
"(g)General date/short time:. {0:g}\n" +
"(G)General date/long time: . {0:G}\n" +
" (default):.. . . . . . . {0} (default = 'G')\n" +
"(M)Month:. . . . . . . . . . {0:M}\n" +
"(R)RFC1123:. . . . . . . . . {0:R}\n" +
"(s)Sortable: . . . . . . . . {0:s}\n" +
"(u)Universal sortable: . . . {0:u} (invariant)\n" +
"(U)Universal sortable: . . . {0:U}\n" +
"(Y)Year: . . . . . . . . . . {0:Y}\n",
thisDate);
Console.WriteLine(s);
//Format a Color enumeration value in various ways.
Console.WriteLine("StandardEnumeration Format Specifiers");
s= String.Format(
"(G)General:. . . . . . . . . {0:G}\n" +
" (default):.. . . . . . . {0} (default = 'G')\n" +
"(F)Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
"(D)Decimal number: . . . . . {0:D}\n" +
"(X)Hexadecimal:. . . . . . . {0:X}\n",
Color.Green);
Console.WriteLine(s);
Console.ReadLine();
}
}
以上代码结果:
Standard Numeric Format Specifiers
(C) Currency: . . . .. . . . ¥-123.00
(D) Decimal:. . . . . . . . . -123
(E) Scientific: . . . . . . . -1.234500E+002
(F) Fixed point:. . . . . . . -123.45
(G) General:. . . . . . . . . -123
(default):.. . . . . . . -123 (default ='G')
(N) Number: . . . . . . . . . -123.00
(P) Percent:. . . . . . . . . -12,345.00%
(R) Round-trip: . . . . . . . -123.45
(X) Hexadecimal:. . . . . . . FFFFFF85
Standard DateTime Format Specifiers
(d) Short date: . . . . . . . 2008-11-1
(D) Long date:. . . .. . . . 2008 年11 月1 日
(t) Short time: . . . . . . . 23:39
(T) Long time:. . . . . . . . 23:39:05
(f) Full date/shorttime: . . 2008 年11 月1 日23:39
(F) Full date/longtime:. . . 2008 年11 月1 日23:39:05
(g) General date/short time:. 2008-11-1 23:39
(G) General date/long time: . 2008-11-123:39:05
(default):.. . . . . . . 2008-11-1 23:39:05 (default ='G')
(M) Month:. . . . . .. . . . 11 月1 日
(R) RFC1123:. . . . . . . . . Sat, 01 Nov 2008 23:39:05GMT
(s) Sortable: . . . . . . . .2008-11-01T23:39:05
(u) Universal sortable: . . . 2008-11-01 23:39:05Z(invariant)
(U) Universalsortable: . . . 2008 年11 月1 日15:39:05
(Y) Year: . . . . . .. . . . 2008 年11 月
Standard Enumeration Format Specifiers
(G) General:. . . . . . . . . Green
(default):.. . . . . . . Green (default ='G')
(F) Flags:. . . . . . . . . . Green (flags orinteger)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003
C#格式化数值结果表
字符
说明
示例
输出
C
货币
string.Format ("{0:C3}",2)
$2.000
D
十进制
string.Format ("{0:D3}",2)
002
E
科学计数法
1.20E+001
1.20E+001
G
常规
string.Format ("{0:G}",2)
2
N
用分号隔开的数字
string.Format ("{0:N}",250000)
250,000.00
X
十六进制
string.Format ("{0:X000}",12)
C
字符型转换 转为字符串
12345.ToString("n"); //生成 12,345.00
12345.ToString("C"); //生成 ¥12,345.00
12345.ToString("e"); //生成 1.234500e+004
12345.ToString("f4"); //生成 12345.0000
12345.ToString("x"); //生成 3039 (16进制)
12345.ToString("p"); //生成 1,234,500.00%
原文地址http://blog.sina.com.cn/s/blog_52ee6fc30100bgmx.html
class Program
以上代码结果:
Standard Numeric Format Specifiers
(C) Currency: . . . .. . . .
(D) Decimal:. . . . . . . . . -123
(E) Scientific: . . . . . . . -1.234500E+002
(F) Fixed point:. . . . . . . -123.45
(G) General:. . . . . . . . . -123
(N) Number: . . . . . . . . . -123.00
(P) Percent:. . . . . . . . . -12,345.00%
(R) Round-trip: . . . . . . . -123.45
(X) Hexadecimal:. . . . . . . FFFFFF85
Standard DateTime Format Specifiers
(d) Short date: . . . . . . . 2008-11-1
(D) Long date:. . . .. . . . 2008
(t) Short time: . . . . . . . 23:39
(T) Long time:. . . . . . . . 23:39:05
(f) Full date/shorttime: . . 2008
(F) Full date/longtime:. . . 2008
(g) General date/short time:. 2008-11-1 23:39
(G) General date/long time: . 2008-11-123:39:05
(M) Month:. . . . . .. . . . 11
(R) RFC1123:. . . . . . . . . Sat, 01 Nov 2008 23:39:05GMT
(s) Sortable: . . . . . . . .2008-11-01T23:39:05
(u) Universal sortable: . . . 2008-11-01 23:39:05Z(invariant)
(U) Universalsortable: . . . 2008
(Y) Year: . . . . . .. . . . 2008
Standard Enumeration Format Specifiers
(G) General:. . . . . . . . . Green
(F) Flags:. . . . . . . . . . Green (flags orinteger)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003
C#格式化数值结果表
C
D
E
N
X
字符型转换 转为字符串
12345.ToString("n"); //生成 12,345.00
12345.ToString("C"); //生成 ¥12,345.00
12345.ToString("e"); //生成 1.234500e+004
12345.ToString("f4"); //生成 12345.0000
12345.ToString("x"); //生成 3039 (16进制)
12345.ToString("p"); //生成 1,234,500.00%
原文地址http://blog.sina.com.cn/s/blog_52ee6fc30100bgmx.html