Delphi format的用法

上一篇 / 下一篇  2008-05-05 23:59:52 / 个人分类:Delphi

一、Format函数的用法51Testing软件测试网J(Is r \v[UF
Format是一个很常用,却又似乎很烦的方法,本人试图对这个方法的帮助进行一些翻译,让它有一个完整的概貌,以供大家查询之用:51Testing软件测试网O;D2i4s3~9x
首先看它的声明:
%^ Z-yo6Sz"E&f0|u0function Format(const Format: string; const Args: array of const): string; overload;51Testing软件测试网/fGwe TJ^x%@ oa5R
事实上Format方法有两个种形式,另外一种是三个参数的,主要区别在于它是线程安全的,但并不多用,所以这里只对第一个介绍:51Testing软件测试网0G6NX/u%UJ KQ
51Testing软件测试网.MW+M/~ayEa-@O
function Format(const Format: string; const Args: array of const): string; overload;51Testing软件测试网?k'{v d.j2y7T K,p,r
Format参数是一个格式字符串,用于格式化Args里面的值的。Args又是什么呢,它是一个变体数组,即它里面可以有多个参数,而且每个参数可以不同。51Testing软件测试网;Y7p-m(s^R1k
如以下例子:51Testing软件测试网 rCU4^xF)|1j
Format('my name is %6s',['wind']);51Testing软件测试网}9g)X5X_6`Wr
返回后就是
a&Oh'i:M0my name is wind
7g m X_ ?j0
zT*W ?9m&_PF$g8D7U0现在来看Format参数的详细情况:
ja:]*e{f"?0Format里面可以写普通的字符串,比如'my name is' 但有些格式指令字符具有特殊意义,比如"%6s"51Testing软件测试网c v9j#|?'ly8o

UOk_ G7Tx0格式指令具有以下的形式:51Testing软件测试网+O{'`8c1qldh`
"%" [index ":"] ["-"] [width] ["." prec] type51Testing软件测试网*Jk R%aL"B bX
它是以"%"开始,而以type结束,type表示一个具体的类型。中间是用来格式化type类型的指令字符,是可选的。
&NUw#{'j051Testing软件测试网G#y4A1FCo
先来看看type,type可以是以下字符:51Testing软件测试网4_ l(kF(q'tT
d 十制数,表示一个整型值
,Le&n oL8Zev0u 和d一样是整型值,但它是无符号的,而如果它对应的值是负的,则返回时是一个2的32次方减去这个绝对值的数51Testing软件测试网l^)\ j H8Fp
   如:Format('this is %u',[-2]);
7I@~(\T uw3x0   返回的是:this is 4294967294
M5u'f1{ G~ hLG0f 对应浮点数51Testing软件测试网GP&KC$h0Y,j L
e 科学表示法,对应整型数和浮点数,51Testing软件测试网k p.B-k%I)l&{
   比如Format('this is %e',[-2.22]);
|XuF7a3?0   返回的是:this is -2.22000000000000E+000
-yOm\S\4x0   等一下再说明如果将数的精度缩小
5W%x&ejD8y~ K5xl0g 这个只能对应浮点型,且它会将值中多余的数去掉51Testing软件测试网z)a8tI(D"l p
   比如Format('this is %g',[02.200]);
)}!o3\N C?p-{6uy0   返回的是:this is 2.2
sx4vP ~8AP0n 只能对应浮点型,将值转化为号码的形式。看一个例子就明白了51Testing软件测试网p"~oo l S^
   Format('this is %n',[4552.2176]);51Testing软件测试网reJ1q8ly
   返回的是this is 4,552.2251Testing软件测试网 F ^-NU+[!}V%Uf
   注意有两点,一是只表示到小数后两位,等一下说怎么消除这种情况
@3r @#WBM'z0   二是,即使小数没有被截断,它也不会也像整数部分一样有逗号来分开的51Testing软件测试网4a5K _?"}h)O p"N4~
m 钱币类型,但关于货币类型有更好的格式化方法,这里只是简单的格式化
Y M$a qd~*bK u0   另外它只对应于浮点值
p1D5d-i2u:~0   Format('this is %m',[9552.21]);
-}c@J/o2D0返回:this is ¥9,552.21
o+l0mJ9E)BAU!a0p 对应于指针类型,返回的值是指针的地址,以十六进制的形式来表示
)Y'Q;y#h!]lh0   例如:
^&`$C J I0   var X:integer;
F@`!K^4J#})bB0     p:^integer;
8s Yx&A3L$i0O0   begin51Testing软件测试网lJB1h,MoBp
    X:=99;
{^V2b`0    p:=@X;
h3e3ArA*e KO0    Edit1.Text:=Format('this is %p',[p]);51Testing软件测试网R&m5M Dd2LZ{
   end;51Testing软件测试网rf[I~V`2]
   Edit1的内容是:this is 0012F548
0j| _~7MB*l,lr)q0s 对应字符串类型,不用多说了吧51Testing软件测试网 _,X&|S]
x 必须是一个整形值,以十六进制的形式返回51Testing软件测试网2]*[J;|T
   Edit1.Text:=Format('this is %X',[15]);51Testing软件测试网_8Ax+}0XY
   返回是:this is F51Testing软件测试网*T }F(i,G5D_"IX/bcp
51Testing软件测试网tD|#w(K\
类型讲述完毕,下面介绍格式化Type的指令:51Testing软件测试网5v vWh@U(` \e
[index ":"] 这个要怎么表达呢,看一个例子51Testing软件测试网,RCO;zG}(y Yc*d5U
             Format('this is %d %d',[12,13]);
*ZPC?F0}q b0             其中第一个%d的索引是0,第二个%d是1,所以字符显示的时候
2uG p ?z:w&P_:`0             是这样 this is 12 13
+[ U"[ }ES+kJ051Testing软件测试网Yk!`^4c
             而如果你这样定义:51Testing软件测试网H \n{vUF nb d
             Format('this is %1:d %0:d',[12,13]);
_B;}9Y4J&i0             那么返回的字符串就变成了51Testing软件测试网R b&~+c$A_$?
             this is 13 1251Testing软件测试网2e*?/N A MVP;J
             现在明白了吗,[index ":"] 中的index指示Args中参数显示的
Nl&Ml5q2l0             顺序51Testing软件测试网"fs}Kb!c*VG

e/wnaRLxF)m0             还有一种情况,如果这样Format('%d %d %d %0:d %d', [1, 2, 3, 4])
Yt!HH|6awr0             将返回1 2 3 1 2。51Testing软件测试网 mo+k,G#\_d
             如果你想返回的是1 2 3 1 4,必须这样定:
R&J1T$YH6T0             Format('%d %d %d %0:d %3:d', [1, 2, 3, 4])51Testing软件测试网q&a!T:Vf*s6q7e3|$y&vH
             但用的时候要注意,索引不能超出Args中的个数,不然会引起异常51Testing软件测试网)l"D%bN}D9]c#v
             如Format('this is %2:d %0:d',[12,13]);51Testing软件测试网9t_*L["mcv
             由于Args中只有12 13 两个数,所以Index只能是0或1,这里为2就错了
#Z%MY/o9HQYxx)h0[width] 指定将被格式化的值占的宽度,看一个例子就明白了51Testing软件测试网sKv6Y*P+mv ]fH*D
         Format('this is %4d',[12]);
Z~"S`.])\0         输出是:this is    1251Testing软件测试网n?Wu?x Z$_&G
         这个是比较容易,不过如果Width的值小于参数的长度,则没有效果。
^r Z2Q Ikh wP0         如:Format('this is %1d',[12]);51Testing软件测试网u2T'G @Ja
         输出是:this is 1251Testing软件测试网%m&n Zw kp\
["-"]   这个指定参数向左齐,和[width]合在一起最可以看到效果:
"RsnPo0Cp0        Format('this is %-4d,yes',[12]);
j};TQCGZ,Q#oP2Y0        输出是:this is 12    ,yes51Testing软件测试网/Jt4wY*CEY
       51Testing软件测试网p M3Vt2v&C:HU
["." prec] 指定精度,对于浮点数效果最佳:
Z e%k EH;M,|:z:H0            Format('this is %.2f',['1.1234]);
M}H&_C*Ls0            输出 this is 1.1251Testing软件测试网 f"@&mBJ
            Format('this is %.7f',['1.1234]);51Testing软件测试网 lp^ hU_k*\]0W p
            输了 this is 1.1234000
$P_,`zyZg}6vQy051Testing软件测试网o U%E/@ ?/O;Gd8n
            而对于整型数,如果prec比如整型的位数小,则没有效果反之比整形值的位数大,则会在整型值的前面以0补之
8[*?;sIp.gyq0            Format('this is %.7d',[1234]);
n-^I D%a*A H0            输出是:this is 0001234]51Testing软件测试网"_].n9tqR#}*]0VT
         51Testing软件测试网U&ck+l)F3} \.]
            对于字符型,刚好和整型值相反,如果prec比字符串型的长度大则没有效果,反之比字符串型的长度小,则会截断尾部的字符
"p"I$M8RpW@X#V0            Format('this is %.2s',['1234']);
x1Br-RaG0            输出是 this is 1251Testing软件测试网l+v(u ]{yZ-g
           
3cOu6U'IS!]0            而上面说的这个例子:51Testing软件测试网9G VRMO#q
            Format('this is %e',[-2.22]);
w0kY)|kkx0            返回的是:this is -2.22000000000000E+000
4\8kO3V;]0Ysy0            怎么去掉多余的0呢,这个就行啦
NK;jK+NI"Kb3F0            Format('this is %.2e',[-2.22]);
%HA2@Jj3RDy0
4FgU3O h1d0二 FormatDateTime的用法
3TVHy1f%~0他的声明为:51Testing软件测试网?!mUl V
function FormatDateTime(const Format: string; DateTime: TDateTime): string; overload;
rg(U3Gx:^0当然和Format一样还有一种,但这里只介绍常用的第一种51Testing软件测试网 g#{)\%~#C$D
Format参数是一个格式化字符串。DateTime是时间类型。返回值是一种格式化后的字符串51Testing软件测试网j9Gk+_Qp,|I|H
51Testing软件测试网#IMM!_pL3c5E
重点来看Format参数中的指令字符
{8n fX+F%X0c 以短时间格式显示时间,即全部是数字的表示
6`wN/bzoQ#SAr H0   FormatdateTime('c',now);
+q WN^7r{i2o0   输出为:2004-8-7 9:55:4051Testing软件测试网Z-z/T)h rXc8s
d 对应于时间中的日期,日期是一位则显示一位,两位则显示两位
0c MX.Eo U(b)}0   FormatdateTime('d',now);51Testing软件测试网6M,Jd4?%i s/s
   输出可能为1~3151Testing软件测试网q!D O,X7u[u
dd 和d的意义一样,但它始终是以两位来显示的
-ck1nthZZl0   FormatdateTime('dd',now);
3ZCR,F!CR e#w'w0    输出可能为01~31
1|8mp7FQ&q~0ddd 显示的是星期几
8Q}|/Z N:@Q0    FormatdateTime('ddd',now);51Testing软件测试网O0R5Ub0bY
    输出为: 星期六
T%?9MFQ'X6kYB0dddd 和ddd显示的是一样的。51Testing软件测试网 Z3R;K/@#x,e'oj
    但上面两个如果在其他国家可能不一样。
yw4~,{;[0ddddd 以短时间格式显示年月日51Testing软件测试网\6L5_#G'C
     FormatdateTime('ddddd',now);
7CoR V4D0     输出为:2004-8-751Testing软件测试网G-\THoUD
dddddd 以长时间格式显示年月日
N{M*S wR0     FormatdateTime('dddddd',now);
%\x7|~Y;M$X? ~4M0     输出为:2004年8月7日51Testing软件测试网:?b+Q0Q+M Z#g9N
e/ee/eee/eeee 以相应的位数显示年51Testing软件测试网.k g2QB1`ZS&l
      FormatdateTime('ee',now);51Testing软件测试网 \4Rev&c8gB
     输出为:04   (表示04年)
am6fd+ou9h0m/mm/mmm/mmmm 表示月
OuLLm~&M2H0      FormatdateTime('m',now);51Testing软件测试网?*o{c2X8t
      输出为:8
E$C'I_3hH @k0      FormatdateTime('mm',now);
'HR_Bn`0      输出为   0851Testing软件测试网mc'V5~Bjp
      FormatdateTime('mmm',now);51Testing软件测试网-_;|htjW/e,m"_
      输出为   八月51Testing软件测试网 e)e&G2R9cQv2LH
      FormatdateTime('mmmm',now);51Testing软件测试网mX]&U"~ nAG u'r:s)Q @
      输出为   八月
H7P2N%W1?!U0     和ddd/dddd 一样,在其他国家可能不同
*IR W'VW.a ?0yy/yyyy 表示年51Testing软件测试网.L8M1o:Vb(i;X,g\$c
      FormatdateTime('yy',now);
Qkb!r*o O/@"u:m0      输出为 0451Testing软件测试网YvO _"?7[q
      FormatdateTime('yyyy',now);
agx:Qt#c0      输出为 2004
J)b#X1Oi8^U0h/hh,n/nn,s/ss,z/zzz 分别表示小时,分,秒,毫秒51Testing软件测试网!T @ e2o2^8n$H@0}
t   以短时间格式显示时间51Testing软件测试网p#k xRX.l n*uGZ+W
      FormatdateTime('t',now);51Testing软件测试网XWK/Qb
     输出为 10:17
&I.ZK+c*ml0tt 以长时间格式显示时间
u9HAy,J+~9v9]0      FormatdateTime('tt',now);
-Y7GSP8Dz(GN"P0      输出为10:18:4651Testing软件测试网8NJn_E/jb.QG
ampm 以长时间格式显示上午还是下午
V5q+`1tT2z9qe0x0      FormatdateTime('ttampm',now);51Testing软件测试网9fB#f]/n2W}
      输出为:10:22:57上午51Testing软件测试网-N(K%A)^1zo

;EU4X,i%q&nn \0大概如此,如果要在Format中加普通的字符串,可以用双引号隔开那些特定义的字符,这样普通字符串中如果含特殊的字符就不会被显示为时间格式啦:51Testing软件测试网 eqc&}l*e
FormatdateTime('"today is" c',now);51Testing软件测试网Yxz?&f*CV`
输出为:today is 2004-8-7 10:26:58
N*n ` Owu0时间中也可以加"-"或"\"来分开日期:
R7e4h-P#{YE6Pn9^p0FormatdateTime('"today is" yy-mm-dd',now);51Testing软件测试网$W+AV&Cxw]
FormatdateTime('"today is" yy\mm\dd',now);51Testing软件测试网f3H8M5UK\YG8s
输出为: today is 04-08-07
_ l#T C R~`Rr0也可以用":"来分开时间  
8z??+{A0FormatdateTime('"today is" hh:nn:ss',now);51Testing软件测试网5F+L4{@syWa!p
输出为:today is 10:32:2351Testing软件测试网RG~-j l;X] xr)a
51Testing软件测试网$?hB9m;V%Zs|
三.FormatFloat的用法51Testing软件测试网3s"t{(Q l

*G*?8W!{4^F0常用的声明:51Testing软件测试网_ A1sL0e?
function FormatFloat(const Format: string; Value: Extended): string; overload;51Testing软件测试网} ac]:Me n
和上面一样Format参数为格式化指令字符,Value为Extended类型为什么是这个类型,因为它是所有浮点值中表示范围最大的,如果传入该方法的参数比如Double或者其他,则可以保存不会超出范围。51Testing软件测试网Q I}O4?{
51Testing软件测试网Gcs5I;k4bd:DY _M
关键是看Format参数的用法
eqNQ f1X-UU$O1^4g00   这个指定相应的位数的指令。51Testing软件测试网w#]/M!A,kC^e1m
    比如:FormatFloat('000.000',22.22);51Testing软件测试网9C#NGY y1Q d4tl~
    输出的就是022.220
O|zE%O0    注意一点,如果整数部分的0的个数小于Value参数中整数的位数,则没有效果51Testing软件测试网Eu(L8| X~r
    如:FormatFloat('0.00',22.22);51Testing软件测试网8Cnk~9q[-x
    输出的是:22.2251Testing软件测试网"_-bJ-qz#b;bE(W^
    但如果小数部分的0小于Value中小数的倍数,则会截去相应的小数和位数51Testing软件测试网a n*b@Qh'\ WD
    如:FormatFloat('0.0',22.22);51Testing软件测试网Q0@7g7k7a[(O[
    输出的是:22.2
Y1jf2R"K MZa0D0   51Testing软件测试网{:yW&ka!e'F;J
    也可以在整数0中指定逗号,这个整数位数必须大于3个,才会有逗号出现51Testing软件测试网$y|HU)m$[
    FormatFloat('0,000.0',2222.22);
ZaTxB d0    输出是:2,222.2
$ai]E`^P0    如果这样FormatFloat('000,0.0',2222.22);
GV*?Z$b0    它的输出还是:2,222.251Testing软件测试网.b.a0a6KxBo|
    注意它的规律
-?H.a&e B0
ETk:R:H6ZG0#   和0的用法一样,目前我还没有测出有什么不同。
@B9K.qR+m9k ?9I b }0    FormatFloat('##.##',22.22);
i9H"V#k~[0    输出是:22.0051Testing软件测试网;D.?+A&LP IT#[A9|

!h[C'V vQ X"j0E   科学表示法,看几个例子大概就明白了51Testing软件测试网 S~ qY|f
    FormatFloat('0.00E+00',2222.22);51Testing软件测试网,fdF\A
    输出是 2.22E+03
#z%\^,h6g0    FormatFloat('0000.00E+00',2222.22);51Testing软件测试网] H1G/pZD5{
    输出是 2222.22E+00
c.Z0FNw0     FormatFloat('00.0E+0',2222.22);51Testing软件测试网'[nb6n MC-w
    22.2E+251Testing软件测试网.|g/o&?%|:kZ
    明白了吗,全靠E右边的0来支配的。

TAG: Delphi

 

评分:0

我来说两句

Open Toolbar