String.Format

本文介绍了一个使用C#进行数值、日期时间和枚举类型格式化的示例。通过不同的格式化符号展示了如何将数值转换为货币、科学计数法等形式;如何显示日期时间的不同格式如长日期、短日期等;以及如何展示枚举类型的多种表现形式。
  1. using System;
  2. class Sample 
  3. {
  4.     enum Color {Yellow = 1, Blue, Green};
  5.     static DateTime thisDate = DateTime.Now;
  6.     public static void Main() 
  7.     {
  8. // Store the output of the String.Format method in a string.
  9.     string s = "";
  10.     Console.Clear();
  11. // Format a negative integer or floating-point number in various ways.
  12.     Console.WriteLine("Standard Numeric Format Specifiers");
  13.     s = String.Format(
  14.         "(C) Currency: . . . . . . . . {0:C}/n" +
  15.         "(D) Decimal:. . . . . . . . . {0:D}/n" +
  16.         "(E) Scientific: . . . . . . . {1:E}/n" +
  17.         "(F) Fixed point:. . . . . . . {1:F}/n" +
  18.         "(G) General:. . . . . . . . . {0:G}/n" +
  19.         "    (default):. . . . . . . . {0} (default = 'G')/n" +
  20.         "(N) Number: . . . . . . . . . {0:N}/n" +
  21.         "(P) Percent:. . . . . . . . . {1:P}/n" +
  22.         "(R) Round-trip: . . . . . . . {1:R}/n" +
  23.         "(X) Hexadecimal:. . . . . . . {0:X}/n",
  24.         -123, -123.45f); 
  25.     Console.WriteLine(s);
  26. // Format the current date in various ways.
  27.     Console.WriteLine("Standard DateTime Format Specifiers");
  28.     s = String.Format(
  29.         "(d) Short date: . . . . . . . {0:d}/n" +
  30.         "(D) Long date:. . . . . . . . {0:D}/n" +
  31.         "(t) Short time: . . . . . . . {0:t}/n" +
  32.         "(T) Long time:. . . . . . . . {0:T}/n" +
  33.         "(f) Full date/short time: . . {0:f}/n" +
  34.         "(F) Full date/long time:. . . {0:F}/n" +
  35.         "(g) General date/short time:. {0:g}/n" +
  36.         "(G) General date/long time: . {0:G}/n" +
  37.         "    (default):. . . . . . . . {0} (default = 'G')/n" +
  38.         "(M) Month:. . . . . . . . . . {0:M}/n" +
  39.         "(R) RFC1123:. . . . . . . . . {0:R}/n" +
  40.         "(s) Sortable: . . . . . . . . {0:s}/n" +
  41.         "(u) Universal sortable: . . . {0:u} (invariant)/n" +
  42.         "(U) Universal sortable: . . . {0:U}/n" +
  43.         "(Y) Year: . . . . . . . . . . {0:Y}/n"
  44.         thisDate);
  45.     Console.WriteLine(s);
  46. // Format a Color enumeration value in various ways.
  47.     Console.WriteLine("Standard Enumeration Format Specifiers");
  48.     s = String.Format(
  49.         "(G) General:. . . . . . . . . {0:G}/n" +
  50.         "    (default):. . . . . . . . {0} (default = 'G')/n" +
  51.         "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)/n" +
  52.         "(D) Decimal number: . . . . . {0:D}/n" +
  53.         "(X) Hexadecimal:. . . . . . . {0:X}/n"
  54.         Color.Green);       
  55.     Console.WriteLine(s);
  56.     }
  57. }
  58. /*
  59. This code example produces the following results:
  60. Standard Numeric Format Specifiers
  61. (C) Currency: . . . . . . . . ($123.00)
  62. (D) Decimal:. . . . . . . . . -123
  63. (E) Scientific: . . . . . . . -1.234500E+002
  64. (F) Fixed point:. . . . . . . -123.45
  65. (G) General:. . . . . . . . . -123
  66.     (default):. . . . . . . . -123 (default = 'G')
  67. (N) Number: . . . . . . . . . -123.00
  68. (P) Percent:. . . . . . . . . -12,345.00 %
  69. (R) Round-trip: . . . . . . . -123.45
  70. (X) Hexadecimal:. . . . . . . FFFFFF85
  71. Standard DateTime Format Specifiers
  72. (d) Short date: . . . . . . . 6/26/2004
  73. (D) Long date:. . . . . . . . Saturday, June 26, 2004
  74. (t) Short time: . . . . . . . 8:11 PM
  75. (T) Long time:. . . . . . . . 8:11:04 PM
  76. (f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
  77. (F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
  78. (g) General date/short time:. 6/26/2004 8:11 PM
  79. (G) General date/long time: . 6/26/2004 8:11:04 PM
  80.     (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
  81. (M) Month:. . . . . . . . . . June 26
  82. (R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
  83. (s) Sortable: . . . . . . . . 2004-06-26T20:11:04
  84. (u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
  85. (U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM
  86. (Y) Year: . . . . . . . . . . June, 2004
  87. Standard Enumeration Format Specifiers
  88. (G) General:. . . . . . . . . Green
  89.     (default):. . . . . . . . Green (default = 'G')
  90. (F) Flags:. . . . . . . . . . Green (flags or integer)
  91. (D) Decimal number: . . . . . 3
  92. (X) Hexadecimal:. . . . . . . 00000003
  93. */
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值