|
Format是一个很常用,却又似乎很烦的方法,本人试图对这个方法的帮助进行一些翻译,让它有一个完整的概貌,以供大家查询之用:wicLinux联盟 wicLinux联盟 首先看它的声明:wicLinux联盟
wicLinux联盟 function Format(const Format: string; const Args: array of const): string; overload;wicLinux联盟
|
wicLinux联盟 事实上Format方法有两个种形式,另外一种是三个参数的,主要区别在于它是线程安全的,wicLinux联盟 但并不多用,所以这里只对第一个介绍:wicLinux联盟
wicLinux联盟 function Format(const Format: string; const Args: array of const): string; overload;wicLinux联盟
|
wicLinux联盟 Format参数是一个格式字符串,用于格式化Args里面的值的。Args又是什么呢,wicLinux联盟 它是一个变体数组,即它里面可以有多个参数,而且每个参数可以不同。wicLinux联盟 如以下例子:wicLinux联盟
wicLinux联盟 Format('my name is %6s',['wind']);wicLinux联盟
|
返回后就是my name is windwicLinux联盟 wicLinux联盟 现在来看Format参数的详细情况:wicLinux联盟 Format里面可以写普通的字符串,比如'my name is',但有些格式指令字符具有特殊意义,比如"%6s"格式指令具有以下的形式:wicLinux联盟 "%" [index ":"] ["-"] [width] ["." prec] typewicLinux联盟 它是以"%"开始,而以type结束,type表示一个具体的类型。中间是用来wicLinux联盟 格式化type类型的指令字符,是可选的。wicLinux联盟 wicLinux联盟 先来看看type,type可以是以下字符:wicLinux联盟 d 十制数,表示一个整型值wicLinux联盟 u 和d一样是整型值,但它是无符号的,而如果它对应的值是负的,则返回时是一个2的32次方减去这个绝对值的数,如:wicLinux联盟
wicLinux联盟 Format('this is %u',[-2]);wicLinux联盟
|
返回的是:this is 4294967294wicLinux联盟 wicLinux联盟 f 对应浮点数wicLinux联盟 e 科学表示法,对应整型数和浮点数,比如wicLinux联盟
wicLinux联盟 Format('this is %e',[-2.22]);wicLinux联盟
|
返回的是:this is -2.22000000000000E+000,等一下再说明如果将数的精度缩小wicLinux联盟 wicLinux联盟 g 这个只能对应浮点型,且它会将值中多余的数去掉,比如wicLinux联盟
wicLinux联盟 Format('this is %g',[02.200]);wicLinux联盟
|
返回的是:this is 2.2wicLinux联盟 wicLinux联盟 n 只能对应浮点型,将值转化为号码的形式。看一个例子就明白了wicLinux联盟
wicLinux联盟 Format('this is %n',[4552.2176]);wicLinux联盟
|
返回的是this is 4,552.22wicLinux联盟 wicLinux联盟 注意有两点,一是只表示到小数后两位,等一下说怎么消除这种情况, 二是,即使小数没有被截断,它也不会也像整数部分一样有逗号来分开的m钱币类型,但关于货币类型有更好的格式化方法,这里只是简单的格式化,另外它只对应于浮点值wicLinux联盟
wicLinux联盟 Format('this is %m',[9552.21]);wicLinux联盟
|
返回:this is ¥9,552.21wicLinux联盟 wicLinux联盟 p 对应于指针类型,返回的值是指针的地址,以十六进制的形式来表示wicLinux联盟 例如:wicLinux联盟
wicLinux联盟 var X:integer;wicLinux联盟 p:^integer;wicLinux联盟 beginwicLinux联盟 X:=99;wicLinux联盟 p:=@X;wicLinux联盟 Edit1.Text:=Format('this is %p',[p]);wicLinux联盟 end;wicLinux联盟
|
Edit1的内容是:this is 0012F548wicLinux联盟 wicLinux联盟 s 对应字符串类型,不用多说了吧wicLinux联盟 x 必须是一个整形值,以十六进制的形式返回wicLinux联盟
wicLinux联盟 Edit1.Text:=Format('this is %X',[15]);wicLinux联盟
|
返回是:this is FwicLinux联盟 wicLinux联盟 类型讲述完毕,下面介绍格式化Type的指令:wicLinux联盟 [index ":"]这个要怎么表达呢,看一个例子wicLinux联盟
wicLinux联盟 Format('this is %d %d',[12,13]);wicLinux联盟
|
其中第一个%d的索引是0,第二个%d是1,所以字符显示的时候是这样 this is 12 13wicLinux联盟 wicLinux联盟 而如果你这样定义:wicLinux联盟
wicLinux联盟 Format('this is %1:d %0:d',[12,13]);wicLinux联盟
|
那么返回的字符串就变成了this is 13 12。现在明白了吗,[index ":"] 中的index指示Args中参数显示的顺序还有一种情况,如果这样wicLinux联盟
wicLinux联盟 Format('%d %d %d %0:d %d', [1, 2, 3, 4])wicLinux联盟
|
将返回1 2 3 1 2。wicLinux联盟 wicLinux联盟 如果你想返回的是1 2 3 1 4,必须这样定:wicLinux联盟
wicLinux联盟 Format('%d %d %d %0:d %3:d', [1, 2, 3, 4])wicLinux联盟
|
wicLinux联盟 但用的时候要注意,索引不能超出Args中的个数,不然会引起异常如wicLinux联盟
wicLinux联盟 Format('this is %2:d %0:d',[12,13]);wicLinux联盟
|
wicLinux联盟 由于Args中只有12 13 两个数,所以Index只能是0或1,这里为2就错了[width] 指定将被格式化的值占的宽度,看一个例子就明白了wicLinux联盟
wicLinux联盟 Format('this is %4d',[12]);wicLinux联盟
|
输出是:this is 12,这个是比较容易,不过如果Width的值小于参数的长度,则没有效果。wicLinux联盟 如:wicLinux联盟
wicLinux联盟 Format('this is %1d',[12]);wicLinux联盟
|
输出是:this is 12wicLinux联盟 wicLinux联盟 ["-"]这个指定参数向左齐,和[width]合在一起最可以看到效果:wicLinux联盟
wicLinux联盟 Format('this is %-4d,yes',[12]);wicLinux联盟
|
输出是:this is 12 ,yeswicLinux联盟 wicLinux联盟 ["." prec] 指定精度,对于浮点数效果最佳:wicLinux联盟
wicLinux联盟 Format('this is %.2f',['1.1234]);wicLinux联盟
|
输出 this is 1.12wicLinux联盟 wicLinux联盟 wicLinux联盟
wicLinux联盟 Format('this is %.7f',['1.1234]);wicLinux联盟
|
输出了 this is 1.1234000wicLinux联盟 wicLinux联盟 而对于整型数,如果prec比如整型的位数小,则没有效果反之比整形值的位数大,则会在整型值的前面以0补之wicLinux联盟 wicLinux联盟
wicLinux联盟 Format('this is %.7d',[1234]);wicLinux联盟
|
输出是:this is 0001234]wicLinux联盟 wicLinux联盟 对于字符型,刚好和整型值相反,如果prec比字符串型的长度大则没有效果,反之比字符串型的长度小,则会截断尾部的字符wicLinux联盟
wicLinux联盟 Format('this is %.2s',['1234']);wicLinux联盟
|
输出是 this is 12,而上面说的这个例子:wicLinux联盟 wicLinux联盟 wicLinux联盟
wicLinux联盟 Format('this is %e',[-2.22]);wicLinux联盟
|
返回的是:this is -2.22000000000000E+000,怎么去掉多余的0呢,这个就行啦wicLinux联盟 wicLinux联盟 wicLinux联盟
wicLinux联盟 Format('this is %.2e',[-2.22]);wicLinux联盟
|
wicLinux联盟 wicLinux联盟 好了,第一个总算讲完了,应该对他的应用很熟悉了吧wicLinux联盟 wicLinux联盟 ///////////////////////////////////////////////////////////////wicLinux联盟 二 FormatDateTime的用法wicLinux联盟 他的声明为:wicLinux联盟
wicLinux联盟 function FormatDateTime(const Format: string; DateTime: TDateTime): string; wicLinux联盟 overload;wicLinux联盟
|
wicLinux联盟 当然和Format一样还有一种,但这里只介绍常用的第一种,Format参数是一个格式化字符串。DateTime是时间类型。返回值是一种格式化后的字符串,重点来看Format参数中的指令字符wicLinux联盟 wicLinux联盟 c 以短时间格式显示时间,即全部是数字的表示wicLinux联盟
wicLinux联盟 FormatdateTime('c',now);wicLinux联盟
|
输出为:2004-8-7 9:55:40wicLinux联盟 wicLinux联盟 d 对应于时间中的日期,日期是一位则显示一位,两位则显示两位wicLinux联盟
wicLinux联盟 FormatdateTime('d',now);wicLinux联盟
|
输出可能为1~31wicLinux联盟 wicLinux联盟 dd 和d的意义一样,但它始终是以两位来显示的wicLinux联盟
wicLinux联盟 FormatdateTime('dd',now);wicLinux联盟
|
输出可能为01~31wicLinux联盟 wicLinux联盟 ddd 显示的是星期几wicLinux联盟
wicLinux联盟 FormatdateTime('ddd',now);wicLinux联盟
|
输出为: 星期六wicLinux联盟 dddd 和ddd显示的是一样的。 但上面两个如果在其他国家可能不一样。ddddd 以短时间格式显示年月日 wicLinux联盟
wicLinux联盟 FormatdateTime('ddddd',now);wicLinux联盟
|
输出为:2004-8-7wicLinux联盟 wicLinux联盟 dddddd 以长时间格式显示年月日wicLinux联盟
wicLinux联盟 FormatdateTime('dddddd',now); wicLinux联盟
|
输出为:2004年8月7日wicLinux联盟 wicLinux联盟 e/ee/eee/eeee 以相应的位数显示年wicLinux联盟
wicLinux联盟 FormatdateTime('ee',now); wicLinux联盟
|
输出为:04 (表示04年)wicLinux联盟 wicLinux联盟 m/mm/mmm/mmmm 表示月wicLinux联盟
wicLinux联盟 FormatdateTime('m',now);wicLinux联盟
|
输出为:8wicLinux联盟 wicLinux联盟 wicLinux联盟
wicLinux联盟 FormatdateTime('mm',now);wicLinux联盟
|
输出为 08wicLinux联盟 wicLinux联盟 wicLinux联盟
wicLinux联盟 FormatdateTime('mmm',now);wicLinux联盟
|
输出为 八月wicLinux联盟 wicLinux联盟 wicLinux联盟
wicLinux联盟 FormatdateTime('mmmm',now); wicLinux联盟
|
输出为 八月wicLinux联盟 wicLinux联盟 和ddd/dddd 一样,在其他国家可能不同yy/yyyy 表示年wicLinux联盟
wicLinux联盟 FormatdateTime('yy',now);wicLinux联盟
|
输出为 04wicLinux联盟 wicLinux联盟 wicLinux联盟
wicLinux联盟 FormatdateTime('yyyy',now);wicLinux联盟
|
输出为 2004,wicLinux联盟 wicLinux联盟 h/hh,n/nn,s/ss,z/zzz 分别表示小时,分,秒,毫秒wicLinux联盟 wicLinux联盟 t 以短时间格式显示时间wicLinux联盟
wicLinux联盟 FormatdateTime('t',now);wicLinux联盟
|
输出为 10:17wicLinux联盟 wicLinux联盟 tt 以长时间格式显示时间wicLinux联盟
wicLinux联盟 FormatdateTime('tt',now);wicLinux联盟
|
输出为10:18:46wicLinux联盟 wicLinux联盟 ampm 以长时间格式显示上午还是下午wicLinux联盟
wicLinux联盟 FormatdateTime('ttampm',now);wicLinux联盟
|
输出为:10:22:57上午wicLinux联盟 wicLinux联盟 大概如此,如果要在Format中加普通的字符串,可以用双引号隔开那些特定义的字符,这样普通字符串中如果含特殊的字符就不会被显示为时间格式啦:wicLinux联盟
wicLinux联盟 FormatdateTime('"today is" c',now);wicLinux联盟
|
输出为:today is 2004-8-7 10:26:58wicLinux联盟 wicLinux联盟 时间中也可以加"-"或"\"来分开日期:wicLinux联盟
wicLinux联盟 FormatdateTime('"today is" yy-mm-dd',now);wicLinux联盟 FormatdateTime('"today is" yy\mm\dd',now);wicLinux联盟
|
输出为: today is 04-08-07wicLinux联盟 wicLinux联盟 也可以用":"来分开时间 wicLinux联盟
wicLinux联盟 FormatdateTime('"today is" hh:nn:ss',now);wicLinux联盟
|
输出为:today is 10:32:23wicLinux联盟 wicLinux联盟 /////////////////////////////////////////////////////////////////wicLinux联盟 三.FormatFloat的用法wicLinux联盟 wicLinux联盟 常用的声明:wicLinux联盟
wicLinux联盟 function FormatFloat(const Format: string; Value: Extended): string; overload;wicLinux联盟
|
wicLinux联盟 和上面一样Format参数为格式化指令字符,Value为Extended类型为什么是这个类型,因为它是所有浮点值中表示范围最大的,如果传入该方法的参数比如Double或者其他,则可以保存不会超出范围。wicLinux联盟 wicLinux联盟 关键是看Format参数的用法wicLinux联盟 0 这个指定相应的位数的指令。wicLinux联盟 比如:wicLinux联盟
wicLinux联盟 FormatFloat('000.000',22.22);wicLinux联盟
|
输出的就是022.220wicLinux联盟 注意一点,如果整数部分的0的个数小于Value参数中整数的位数,则没有效果如:wicLinux联盟
wicLinux联盟 FormatFloat('0.00',22.22);wicLinux联盟
|
输出的是:22.22wicLinux联盟 wicLinux联盟 但如果小数部分的0小于Value中小数的倍数,则会截去相应的小数和位数如:wicLinux联盟
wicLinux联盟 FormatFloat('0.0',22.22);wicLinux联盟
|
输出的是:22.2wicLinux联盟 wicLinux联盟 也可以在整数0中指定逗号,这个整数位数必须大于3个,才会有逗号出句wicLinux联盟
wicLinux联盟 FormatFloat('0,000.0',2222.22);wicLinux联盟
|
输出是:2,222.2wicLinux联盟 wicLinux联盟 如果这样wicLinux联盟
wicLinux联盟 FormatFloat('000,0.0',2222.22);wicLinux联盟
|
它的输出还是:2,222.2wicLinux联盟 wicLinux联盟 注意它的规律,#和0的用法一样,目前我还没有测出有什么不同。wicLinux联盟
wicLinux联盟 FormatFloat('##.##',22.22);wicLinux联盟
|
输出是:22.00wicLinux联盟 wicLinux联盟 E 科学表示法,看几个例子大概就明白了wicLinux联盟
wicLinux联盟 FormatFloat('0.00E+00',2222.22);wicLinux联盟
|
输出是 2.22E+03wicLinux联盟 wicLinux联盟 wicLinux联盟
wicLinux联盟 FormatFloat('0000.00E+00',2222.22);wicLinux联盟
|
输出是 2222.22E+00wicLinux联盟 wicLinux联盟 wicLinux联盟
wicLinux联盟 FormatFloat('00.0E+0',2222.22);wicLinux联盟
|
22.2E+2wicLinux联盟 明白了吗,全靠E右边的0来支配的。wicLinux联盟 wicLinux联盟 这个方法并不难,大概就是这样子了。wicLinux联盟 wicLinux联盟 上面三个方法是很常用的,没有什么技巧,只要记得这些规范就行了。wicLinux联盟 wicLinux联盟 总结一下Format的用法:wicLinux联盟
wicLinux联盟 Format('x=%d',[12]);//'x=12'//最普通wicLinux联盟 Format('x=%3d',[12]);//'x=12'//指定宽度wicLinux联盟 Format('x=%f',[12.0]);//'x=12.00'//浮点数wicLinux联盟 Format('x=%.3f',[12.0]);//'x=12.000'//指定小数wicLinux联盟 Format('x=%8.2f'[12.0])//'x=12.00';wicLinux联盟 Format('x=%.*f',[5,12.0]);//'x=12.00000'//动态配置wicLinux联盟 Format('x=%.5d',[12]);//'x=00012'//前面补充0wicLinux联盟 Format('x=%.5x',[12]);//'x=0000C'//十六进制wicLinux联盟 Format('x=%1:d%0:d',[12,13]);//'x=1312'//使用索引wicLinux联盟 Format('x=%p',[nil]);//'x=00000000'//指针wicLinux联盟 Format('x=%1.1e',[12.0]);//'x=1.2E+001'//科学记数法wicLinux联盟 Format('x=%%',[]);//'x=%'//得到"%"wicLinux联盟 S:=Format('%s%d',[S,I]);//S:=S+StrToInt(I);//连接字符串wicLinux联盟
|
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论 |
|