C# 格式化输出

一、字符串的格式化输出1.1 格式化输出表字符说明示例输出C货币string.Format("{0:C3}", 2)$2.000D十进制string.Format("{0:D3}", 2)002E科学计数法string.Format(“{0:e}”,1.2)1.20E+001G常规string.Format("{0:G}", 2)2N用分号隔开的数字string.Format("{0:N}", 250000)250,000.00X十六进制string.Format("{0:X000}", 12)Cstring.Format("{0:000.000}", 12.2)012.200eg.数字的格式指定可以通过往刚才所说的修饰项目中添加几个参数来实现控制数字格式的目的。下表列出了几个常见的例子。操作格式代码输出固定宽度右对齐String.Format("{0, 4}", num)“   1”固定宽度左对齐String.Format("{0, -4}", num)“1   ”用0填充String.Format("{0:D4}", num)或者String.Format("{0:0000}", num)“0001”固定宽度并用0填充String.Format("{0, 8:D4}", num)“    0001”1.2 StringsThere really isn't any formatting within a strong, beyond it's alignment. Alignment works for any argument being printed in a String.Format call.SampleGeneratesString.Format("->{1,10}<-", "Hello");-> Hello<-String.Format("->{1,-10}<-", "Hello");->Hello <-1.3 NumbersBasic number formatting specifiers:SpecifierTypeFormatOutput(PassedDouble 1.42)Output(PassedInt -12400)cCurrency{0:c}$1.42-$12,400dDecimal (Whole number){0:d}System.FormatException-12400eScientific{0:e}1.420000e+000-1.240000e+004fFixed point{0:f}1.42-12400.00gGeneral{0:g}1.42-12400nNumber with commas for thousands{0:n}1.42-12,400rRound trippable{0:r}1.42System.FormatExceptionxHexadecimal{0:x4}System.FormatExceptioncf90Custom number formatting ( Passed Double 1500.42 ) :SpecifierTypeExampleOutputNote0Zero placeholder{0:00.0000}1500.4200Pads with zeroes.#Digit placeholder{0:(#).##}(1500).42.Decimal point{0:0.0}1500.4,Thousand separator{0:0,0}1,500Must be between two zeroes.,.Number scaling{0:0,.}2Comma adjacent to Period scales by 1000.%Percent{0:0%}150042%Multiplies by 100, adds % sign.eExponent placeholder{0:00e+0}15e+2Many exponent formats available.;Group separatorsee belowThe group separator is especially useful for formatting currency valueswhich require that negative values be enclosed in parentheses. This currencyformatting example at the bottom of this document makes it obvious:1.4 DatesNote that date formatting is especially dependant on the system's regionalsettings; the example strings here are from my local locale.SpecifierTypeExample (Passed System.DateTime.Now)dShort date10/12/2002DLong dateDecember 10, 2002tShort time10:11 PMTLong time10:11:29 PMfFull date & timeDecember 10, 2002 10:11 PMFFull date & time (long)December 10, 2002 10:11:29 PMgDefault date & time10/12/2002 10:11 PMGDefault date & time (long)10/12/2002 10:11:29 PMMMonth day patternDecember 10rRFC1123 date stringTue, 10 Dec 2002 22:11:29 GMTsSortable date string2002-12-10T22:11:29uUniversal sortable, local time2002-12-10 22:13:50ZUUniversal sortable, GMTDecember 11, 2002 3:13:50 AMYYear month patternDecember, 2002The 'U' specifier seems broken; that string certainly isn't sortable.Custom date formatting:SpecifierTypeExampleExample OutputddDay{0:dd}10dddDay name{0:ddd}TueddddFull day name{0:dddd}Tuesdayf, ff, ...Second fractions{0:fff}932gg, ...Era{0:gg}A.D.hh2 digit hour{0:hh}10HH2 digit hour, 24hr format{0:HH}22mmMinute 00-59{0:mm}38MMMonth 01-12{0:MM}12MMMMonth abbreviation{0:MMM}DecMMMMFull month name{0:MMMM}DecemberssSeconds 00-59{0:ss}46ttAM or PM{0:tt}PMyyYear, 2 digits{0:yy}02yyyyYear{0:yyyy}2002zzTimezone offset, 2 digits{0:zz}-05zzzFull timezone offset{0:zzz}-05:00:Separator{0:hh:mm:ss}10:43:20/Separator{0:dd/MM/yyyy}10/12/2002EnumerationsSpecifierTypegDefault (Flag names if available, otherwise decimal)fFlags alwaysdInteger alwaysxEight digit hex.二、Some Useful ExamplesString.Format("{0:$#,##0.00;($#,##0.00);Zero}",value);This will output "$1,240.00" if passed 1243.50. It will output the same format butin parentheses if the number is negative, and will output the string"Zero" if the number is zero.String.Format("{0:(###)###-####}", 18005551212);This will output "(800) 555-1212".变量.ToString()字符型转换 转为字符串12345.ToString("n"); //生成 12,345.0012345.ToString("C"); //生成 ¥12,345.0012345.ToString("e"); //生成 1.234500e+00412345.ToString("f4"); //生成 12345.000012345.ToString("x"); //生成 3039 (16进制)12345.ToString("p"); //生成 1,234,500.00%C#:String.Format数字格式化输出int a = 12345678;//格式为sring输出// Label1.Text = string.Format("asdfadsf{0}adsfasdf",a);// Label2.Text = "asdfadsf"+a.ToString()+"adsfasdf";// Label1.Text = string.Format("asdfadsf{0:C}adsfasdf",a);//asdfadsf¥1,234.00adsfasdf// Label2.Text ="asdfadsf"+a.ToString("C")+"adsfasdf";//asdfadsf¥1,234.00adsfasdfdouble b = 1234.12543;a = 12345678;//格式为特殊的string样式输出// Label1.Text = string.Format("asdfadsf{0:C}adsfasdf",b);//asdfadsf¥1,234.13adsfasdf// Label2.Text ="asdfadsf"+b.ToString("C")+"adsfasdf";//asdfadsf¥1,234.13adsfasdf// Label1.Text = string.Format("{0:C3}",b);//¥1,234.125// Label2.Text = b.ToString("C3");//¥1,234.125// Label1.Text = string.Format("{0:d}",a);//十进制--12345678// Label2.Text = b.ToString("d");//十进制--相同的类型,转换报错// Label1.Text = string.Format("{0:e}",a);//指数--1.234568e+007// Label2.Text = b.ToString("e");//指数--1.234125e+003// Label1.Text = string.Format("{0:f}",a);//定点数--12345678.00// Label2.Text = b.ToString("f");//定点数--1234.13// Label1.Text = string.Format("{0:n}",a);//数值--12,345,678.00// Label2.Text = b.ToString("n");//数值--1,234.13// Label1.Text = string.Format("{0:x}",a);//十六进制--bc614e// Label2.Text = b.ToString("x");//16--带有小数不能转换,出错// Label1.Text = string.Format("{0:g}",a);//通用为最紧凑--12345678// Label2.Text = b.ToString("g");//通用为最紧凑--1234.12543// Label1.Text = string.Format("{0:r}",a);//转来转去不损失精度--整数不允许用,报错// Label2.Text = b.ToString("r");//转来转去不损失精度--1234.12543b = 4321.12543;a = 1234;//自定义模式输出:// 0 描述:占位符,如果可能,填充位// Label1.Text = string.Format("{0:000000}",a);// 001234// Label2.Text = string.Format("{0:000000}",b);// 004321// # 描述:占位符,如果可能,填充位// Label1.Text = string.Format("{0:#######}",a);// 1234// Label2.Text = string.Format("{0:#######}",b);// 4321// Label1.Text = string.Format("{0:#0####}",a);// 01234// Label2.Text = string.Format("{0:0#0000}",b);// 004321// . 描述:小数点// Label1.Text = string.Format("{0:000.000}",a);//1234.000// Label2.Text = string.Format("{0:000.000}",b);//4321.125b = 87654321.12543;a = 12345678;// , 描述:数字分组,也用于增倍器// Label1.Text = string.Format("{0:0,00}",a);//12,345,678// Label2.Text = string.Format("{0:0,00}",b);//87,654,32// Label1.Text = string.Format("{0:0,}",a);//12346// Label2.Text = string.Format("{0:0,}",b);//87654// Label1.Text = string.Format("{0:0,,}",a);//12// Label2.Text = string.Format("{0:0,,}",b);//88// Label1.Text = string.Format("{0:0,,,}",a);//0// Label2.Text = string.Format("{0:0,,,}",b);//0// % 描述:格式为百分数// Label1.Text = string.Format("{0:0%}",a);//1234567800%// Label2.Text = string.Format("{0:#%}",b);//8765432113%// Label1.Text = string.Format("{0:0.00%}",a);// 1234567800.00%// Label2.Text = string.Format("{0:#.00%}",b);// 8765432112.54%// 'abc' 描述:显示单引号内的文本// Label1.Text = string.Format("{0:'文本'0}",a);// 文本12345678// Label2.Text = string.Format("{0:文本 0}",b);// 文本87654321// / 描述: 后跟1要打印字的字符,也用于转移符/n等// Label1.Text = string.Format("/"你好!/"");// "你好!"// Label2.Text = string.Format("//c//books//new//we.asp");///c/books/new/we.asp// @描述:后跟要打印字的字符,// Label1.Text = string.Format(@"""你好!""");// "你好!"要打印"则需要输入两对才可以// Label2.Text = string.Format(@"/c/books/new/we.asp");///c/books/new/we.asp百分数格式应该用“p”这个参数。格式      原始数据    结果"{0:P}" 0.40 40%数字 {0:N2} 12.36数字 {0:N0} 13货币 {0:c2} 12.36货币0:c4 12.36货币0:c412.3656货币 "¥{0:N2}" ¥12.36科学计数法 {0:E3} 1.23E+001百分数 {0:P} 12.25% P and ppresent the same.日 期 {0:D} 2006年11月25日日期 {0:d} 2006-11-25日期 {0:f} 2006年11月25日 10:30日期 {0:F} 2006年11月25日 10:30:00日期 {0:s} 2006-11-26 10:30:00时间 {0:T} 10:30:00DateTime dt = DateTime.Now;Label1.Text = dt.ToString();//2005-11-5 13:21:25Label2.Text = dt.ToFileTime().ToString();//127756416859912816Label3.Text = dt.ToFileTimeUtc().ToString();//127756704859912816Label4.Text = dt.ToLocalTime().ToString();//2005-11-521:21:25Label5.Text = dt.ToLongDateString().ToString();//2005年11月5日Label6.Text = dt.ToLongTimeString().ToString();//13:21:25Label7.Text = dt.ToOADate().ToString();//38661.5565508218Label8.Text = dt.ToShortDateString().ToString();//2005-11-5Label9.Text = dt.ToShortTimeString().ToString();//13:21Label10.Text = dt.ToUniversalTime().ToString();//2005-11-55:21:25Label1.Text = dt.Year.ToString();//2005Label2.Text = dt.Date.ToString();//2005-11-5 0:00:00Label3.Text = dt.DayOfWeek.ToString();//SaturdayLabel4.Text = dt.DayOfYear.ToString();//309Label5.Text = dt.Hour.ToString();//13Label6.Text = dt.Millisecond.ToString();//441Label7.Text = dt.Minute.ToString();//30Label8.Text = dt.Month.ToString();//11Label9.Text = dt.Second.ToString();//28Label10.Text = dt.Ticks.ToString();//632667942284412864Label11.Text = dt.TimeOfDay.ToString();//13:30:28.4412864Label1.Text = dt.ToString();//2005-11-513:47:04Label2.Text = dt.AddYears(1).ToString();//2006-11-513:47:04Label3.Text = dt.AddDays(1.1).ToString();//2005-11-616:11:04Label4.Text = dt.AddHours(1.1).ToString();//2005-11-514:53:04Label5.Text = dt.AddMilliseconds(1.1).ToString();//2005-11-513:47:04Label6.Text = dt.AddMonths(1).ToString();//2005-12-513:47:04Label7.Text = dt.AddSeconds(1.1).ToString();//2005-11-513:47:05

(0)

相关推荐