博 之 文

以 拼 搏 设 计 梦 想 , 以 恒 心 编 程 明 天
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

string.Format()的使用;

Posted on 2012-07-11 15:41  IsNull_Soft  阅读(264)  评论(0)    收藏  举报
using System;

public class Example
{
   public static void Main()
   {
      // Create array of 5-tuples with population data for three U.S. cities, 1940-1950.
      Tuple<string, DateTime, int, DateTime, int>[] cities = 
          { Tuple.Create("Los Angeles", new DateTime(1940, 1, 1), 1504277, 
                         new DateTime(1950, 1, 1), 1970358),
            Tuple.Create("New York", new DateTime(1940, 1, 1), 7454995, 
                         new DateTime(1950, 1, 1), 7891957),  
            Tuple.Create("Chicago", new DateTime(1940, 1, 1), 3396808, 
                         new DateTime(1950, 1, 1), 3620962),  
            Tuple.Create("Detroit", new DateTime(1940, 1, 1), 1623452, 
                         new DateTime(1950, 1, 1), 1849568) };

      // Display header
      string header = String.Format("{0,-12}{1,8}{2,12}{1,8}{2,12}{3,14}\n",
                                    "City", "Year", "Population", "Change (%)");
      Console.WriteLine(header);
      string output;      
      foreach (var city in cities) {
         output = String.Format("{0,-12}{1,8:yyyy}{2,12:N0}{3,8:yyyy}{4,12:N0}{5,14:P1}",
                                city.Item1, city.Item2, city.Item3, city.Item4, city.Item5,
                                (city.Item5 - city.Item3)/city.Item3 * 1.0);
         Console.WriteLine(output);
      }
   }
}
// The example displays the following output:
//    City            Year  Population    Year  Population    Change (%)
//    
//    Los Angeles     1940   1,504,277    1950   1,970,358        31.0 %
//    New York        1940   7,454,995    1950   7,891,957         5.9 %
//    Chicago         1940   3,396,808    1950   3,620,962         6.6 %
//    Detroit         1940   1,623,452    1950   1,849,568        13.9 %
decimal d = 3.3333m;
            decimal f = 4;
            decimal g = d + f;
            decimal I = 99999999999999999999999999m;
            int int1 = 888888888;
            string h = string.Format("我的decimal的值是{0:C}", g);
            string J = String.Format("我的第二个decimal的值是{0:C}", I);
            string int2 = String.Format("我要测试abc={0,12:N0}", int1);
            Response.Write(h + "</br>");
            Response.Write(J + "</br>");
            Response.Write(int2+"</br>");
            Response.Write(String.Format("我是来测试百分率的:{0,14:P1}",0.9333));


输出结果:
我的decimal的值是¥7.33
我的第二个decimal的值是¥99,999,999,999,999,999,999,999,999.00
我要测试abc= 888,888,888
我是来测试百分率的: 93.3%