protected void Button1_Click(object sender, EventArgs e){TextBox1.TextMode TextBoxMode.MultiLine;string str;str string.Format("***{0}***{1}***", "ASP.Net", 3.5);TextBox1.Text str "\n"; //***ASP.Net***3.5*** str string.Format("*{0,10}*", "asp");TextBox1.Text str "\n"; //* asp*str string.Format("*{0,-10}*", "asp");TextBox1.Text str "\n"; //*asp *TextBox1.Text string.Format("{0,3} : {1,-3}\n", "BC", 123); // BC : 123TextBox1.Text string.Format("{0,3} : {1,-3}\n", "C", 12); // C : 12 TextBox1.Text string.Format("{0,3} : {1,-3}\n", "ABC", 1); //ABC : 1 }
标准数字格式:
protected void Button1_Click(object sender, EventArgs e){string s1,s2,s3,s4,s5,s6,s7,s8,s9,s0;double num 2.5;s1 string.Format("Cc {0:c}", num); //Cc ¥2.50s2 string.Format("Dd {0:d}", (int)num); //Dd 2s3 string.Format("E {0:E}", num); //E 2.500000E000s4 string.Format("e {0:e}", num); //e 2.500000e000s5 string.Format("Ff {0:f}", num); //Ff 2.50s6 string.Format("Gg {0:g}", num); //Gg 2.5s7 string.Format("Nn {0:n}", num); //Nn 2.50s8 string.Format("Pp {0:p}", num); //Pp 250.00%s9 string.Format("Rr {0:r}", num); //Rr 2.5s0 string.Format("Xx {0:x}", (int)num); //Xx 2string br "\n";TextBox1.Text string.Concat(s1, br, s2, br, s3, br, s4, br, s5, br, s6, br, s7, br, s8, br, s9, br, s0);}
标准数字格式的精度:
protected void Button1_Click(object sender, EventArgs e){string s1,s2,s3,s4,s5,s6,s7,s8,s9,s0;double num 2.5;s1 string.Format("Cc {0:c3}", num); //Cc ¥2.500s2 string.Format("Dd {0:d3}", (int)num); //Dd 002s3 string.Format("E {0:E3}", num); //E 2.500E000s4 string.Format("e {0:e3}", num); //e 2.500e000s5 string.Format("Ff {0:f3}", num); //Ff 2.500s6 string.Format("Gg {0:g3}", num); //Gg 2.5s7 string.Format("Nn {0:n3}", num); //Nn 2.500s8 string.Format("Pp {0:p3}", num); //Pp 250.000%s9 string.Format("Rr {0:r3}", num); //Rr 2.5s0 string.Format("Xx {0:x3}", (int)num); //Xx 002string br "\n";TextBox1.Text string.Concat(s1, br, s2, br, s3, br, s4, br, s5, br, s6, br, s7, br, s8, br, s9, br, s0);}
自定义的数字格式:
protected void Button1_Click(object sender, EventArgs e){string s1,s2,s3,s4,s5,s6,s7,s8,s9,s0;double num -1234.567;s1 string.Format("{0:000000.00}", num); //-001234.57s2 string.Format("{0:######.##}", num); //-1234.57s3 string.Format("{0:#,#.####}", num); //-1,234.567s4 string.Format("{0:0,0.0000}", num); //-1,234.5670s5 string.Format("{0:######.000000}", num); //-1234.567000s6 string.Format("{0:000000.######}", num); //-001234.567s7 string.Format("{0:#.00%}", num); //-123456.70%s8 string.Format("{0:%#.00}", num); //-%123456.70s9 string.Format("{0:(|0)#.#;(-)#.#}", num); //(-)1234.6s0 string.Format("{0:()#.#;(-)#.#;(0)}", num); //(-)1234.6string br "\n";TextBox1.Text string.Concat(s1, br, s2, br, s3, br, s4, br, s5, br, s6, br, s7, br, s8, br, s9, br, s0);}
标准日期和时间格式:
protected void Button1_Click(object sender, EventArgs e){string[] arr new string[16];DateTime dt new DateTime(2011, 1, 2, 13, 24, 56, 789);arr[0] string.Format(" {0}", dt); // 2011/1/2 13:24:56arr[1] string.Format("d {0:d}", dt); //d 2011/1/2arr[2] string.Format("D {0:D}", dt); //D 2011年1月2日arr[3] string.Format("f {0:f}", dt); //f 2011年1月2日 13:24arr[4] string.Format("F {0:F}", dt); //F 2011年1月2日 13:24:56arr[5] string.Format("g {0:g}", dt); //g 2011/1/2 13:24arr[6] string.Format("G {0:G}", dt); //G 2011/1/2 13:24:56arr[7] string.Format("Mm {0:m}", dt); //Mm 1月2日arr[8] string.Format("Oo {0:o}", dt); //Oo 2011-01-02T13:24:56.7890000arr[9] string.Format("Rr {0:r}", dt); //Rr Sun, 02 Jan 2011 13:24:56 GMTarr[10] string.Format("s {0:s}", dt); //s 2011-01-02T13:24:56arr[11] string.Format("t {0:t}", dt); //t 13:24arr[12] string.Format("T {0:T}", dt); //T 13:24:56arr[13] string.Format("u {0:u}", dt); //u 2011-01-02 13:24:56Zarr[14] string.Format("U {0:U}", dt); //U 2011年1月2日 5:24:56arr[15] string.Format("Yy {0:y}", dt); //Yy 2011年1月string str "";foreach (string s in arr) { str s "\n"; }TextBox1.Text str;}
自定义的日期和时间格式:
protected void Button1_Click(object sender, EventArgs e){string [] arr new string[44];DateTime dt new DateTime(2011, 1, 2, 13, 24, 56, 789);arr[0] string.Format("d {0:d}", dt); //d 2011/1/2arr[1] string.Format("dd {0:dd}", dt); //dd 02arr[2] string.Format("ddd {0:ddd}", dt); //ddd 周日arr[3] string.Format("dddd {0:dddd}", dt); //dddd 星期日arr[4] string.Format("f {0:f}", dt); //f 2011年1月2日 13:24arr[5] string.Format("ff {0:ff}", dt); //ff 78arr[6] string.Format("fff {0:fff}", dt); //fff 789arr[7] string.Format("ffff {0:ffff}", dt); //ffff 7890arr[8] string.Format("fffff {0:fffff}", dt); //fffff 78900arr[9] string.Format("ffffff {0:ffffff}", dt); //ffffff 789000arr[10] string.Format("fffffff {0:fffffff}", dt); //fffffff 7890000arr[11] string.Format("F {0:F}", dt); //F 2011年1月2日 13:24:56arr[12] string.Format("FF {0:FF}", dt); //FF 78arr[13] string.Format("FFF {0:FFF}", dt); //FFF 789arr[14] string.Format("FFFF {0:FFFF}", dt); //FFFF 789arr[15] string.Format("FFFFF {0:FFFFF}", dt); //FFFFF 789arr[16] string.Format("FFFFFF {0:FFFFFF}", dt); //FFFFFF 789arr[17] string.Format("FFFFFFF {0:FFFFFFF}", dt); //FFFFFFF 789arr[18] string.Format("g {0:g}", dt); //g 2011/1/2 13:24arr[19] string.Format("h {0:%h}", dt); //h 1arr[20] string.Format("hh {0:hh}", dt); //hh 01arr[21] string.Format("H {0:%H}", dt); //H 13arr[22] string.Format("HH {0:HH}", dt); //HH 13arr[23] string.Format("K {0:%K}", dt); //K arr[24] string.Format("m {0:m}", dt); //m 1月2日arr[25] string.Format("mm {0:mm}", dt); //mm 24arr[26] string.Format("M {0:M}", dt); //M 1月2日arr[27] string.Format("MM {0:MM}", dt); //MM 01arr[28] string.Format("MMM {0:MMM}", dt); //MMM 一月arr[29] string.Format("MMMM {0:MMMM}", dt); //MMMM 一月arr[30] string.Format("s {0:s}", dt); //s 2011-01-02T13:24:56arr[31] string.Format("ss {0:ss}", dt); //ss 56arr[32] string.Format("t {0:t}", dt); //t 13:24arr[33] string.Format("tt {0:tt}", dt); //tt 下午arr[34] string.Format("y {0:y}", dt); //y 2011年1月arr[35] string.Format("yy {0:yy}", dt); //yy 11arr[36] string.Format("yyy {0:yyy}", dt); //yyy 2011arr[37] string.Format("yyyy {0:yyyy}", dt); //yyyy 2011arr[38] string.Format("yyyyy {0:yyyyy}", dt); //yyyyy 02011arr[39] string.Format("z {0:%z}", dt); //z 8arr[40] string.Format("zz {0:zz}", dt); //zz 08arr[41] string.Format("zzz {0:zzz}", dt); //zzz 08:00arr[42] string.Format("{0:yyyy年MM月dd日 HH时mm分ss秒}", dt); //2011年01月02日 13时24分56秒arr[43] string.Format("{0:yyyy/M/d H:m:s}", dt); //2011/1/2 13:24:56string str "";foreach (string s in arr) { str s "\n"; }TextBox1.Text str;}
按区域格式化:
// 需 using System.Globalization;protected void Button1_Click(object sender, EventArgs e){string s1, s2, s3, s4, s5, s6, s7, s8, s9, s0;double num 2.5;DateTime dt new DateTime(2011, 1, 2, 13, 24, 56, 789);s1 string.Format("{0:c}", num); //¥2.50s2 string.Format(new CultureInfo("en-US"), "{0:c}", num); //$2.50s3 string.Format(new CultureInfo("fr-FR"), "{0:c}", num); //2,50 €s4 string.Format(new CultureInfo("it-IT"), "{0:c}", num); //€ 2,50s5 string.Format(new CultureInfo("de-DE"), "{0:c}", num); //2,50 €s6 string.Format("{0,-5:ddd}\t{0:dddd}", dt); //周日 星期日s7 string.Format(new CultureInfo("en-US"), "{0,-5:ddd}\t{0:dddd}", dt); //Sun Sundays8 string.Format(new CultureInfo("fr-FR"), "{0,-5:ddd}\t{0:dddd}", dt); //dim. dimanches9 string.Format(new CultureInfo("it-IT"), "{0,-5:ddd}\t{0:dddd}", dt); //dom domenicas0 string.Format(new CultureInfo("de-DE"), "{0,-5:ddd}\t{0:dddd}", dt); //So Sonntagstring br "\n";TextBox1.Text string.Concat(s1, br, s2, br, s3, br, s4, br, s5, br, s6, br, s7, br, s8, br, s9, br, s0);}