C# string.Format 格式化使用说明

来源:互联网 发布:centos 启动php 编辑:程序博客网 时间:2024/05/07 01:31

MSDN介绍:

http://msdn.microsoft.com/zh-cn/library/system.string.format.aspx

 

 C#格式化数值结果表

字符

说明

示例

输出

C货币string.Format("{0:C3}", 2)$2.000D十进制string.Format("{0:D3}", 2)002E科学计数法1.20E+0011.20E+001G常规string.Format("{0:G}", 2)2N用分号隔开的数字string.Format("{0:N}", 250000)250,000.00X十六进制string.Format("{0:X000}", 12)C  string.Format("{0:000.000}", 12.2)012.200

Strings

There 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 <-

Numbers

Basic number formatting specifiers:

 

SpecifierTypeFormat

Output 
(Passed 
Double 1.42)

Output 
(Passed 
Int -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.
FormatException
xHexadecimal{0:x4}System.
FormatException
cf90

Custom number formatting:

 

SpecifierTypeExampleOutput (Passed Double 1500.42)Note0Zero 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 below  

The group separator is especially useful for formatting currency values which require that negative values be enclosed in parentheses. This currency formatting example at the bottom of this document makes it obvious:

Dates

Note that date formatting is especially dependant on the system's regional settings; 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, 2002

The '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/2002

Enumerations

 

SpecifierTypegDefault (Flag names if available, otherwise decimal)fFlags alwaysdInteger alwaysxEight digit hex.

Some Useful Examples

String.Format("{0:$#,##0.00;($#,##0.00);Zero}", value);

This will output "$1,240.00" if passed 1243.50. It will output the same format but in 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.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%

 

1、格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元)

string.Format("{0:C}",0.2) 结果为:¥0.20 (英文操作系统结果:$0.20)

默认格式化小数点后面保留两位小数,如果需要保留一位或者更多,可以指定位数
string.Format("{0:C1}",23.15) 结果为:¥23.2 (截取会自动四舍五入)

格式化多个Object实例
string.Format("市场价:{0:C},优惠价{1:C}",23.15,19.82)

2、格式化十进制的数字(格式化成固定的位数,位数不能少于未格式化前,只支持整形)

string.Format("{0:D3}",23) 结果为:023

string.Format("{0:D2}",1223) 结果为:1223,(精度说明符指示结果字符串中所需的最少数字个数。)

3、用分号隔开的数字,并指定小数点后的位数

string.Format("{0:N}", 14200) 结果为:14,200.00 (默认为小数点后面两位)

string.Format("{0:N3}", 14200.2458) 结果为:14,200.246 (自动四舍五入)

4、格式化百分比

string.Format("{0:P}", 0.24583) 结果为:24.58% (默认保留百分的两位小数)

string.Format("{0:P1}", 0.24583) 结果为:24.6% (自动四舍五入)

5、零占位符和数字占位符

string.Format("{0:0000.00}", 12394.039) 结果为:12394.04

string.Format("{0:0000.00}", 194.039) 结果为:0194.04

string.Format("{0:###.##}", 12394.039) 结果为:12394.04

string.Format("{0:####.#}", 194.039) 结果为:194

6、日期格式化

string.Format("{0:d}",System.DateTime.Now) 结果为:2009-3-20 (月份位置不是03)

string.Format("{0:D}",System.DateTime.Now) 结果为:2009年3月20日

string.Format("{0:f}",System.DateTime.Now) 结果为:2009年3月20日 15:37

string.Format("{0:F}",System.DateTime.Now) 结果为:2009年3月20日 15:37:52

string.Format("{0:g}",System.DateTime.Now) 结果为:2009-3-20 15:38

string.Format("{0:G}",System.DateTime.Now) 结果为:2009-3-20 15:39:27

string.Format("{0:m}",System.DateTime.Now) 结果为:3月20日

string.Format("{0:t}",System.DateTime.Now) 结果为:15:41

string.Format("{0:T}",System.DateTime.Now) 结果为:15:41:50




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.00adsfasdf   double 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.12543      b = 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.125   b = 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%