Delphi format的用法

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

一、Format函数的用法51Testing软件测试网fudwl@B3n?"q
Format是一个很常用,却又似乎很烦的方法,本人试图对这个方法的帮助进行一些翻译,让它有一个完整的概貌,以供大家查询之用:51Testing软件测试网~w3\ GToO-p,n2TVX
首先看它的声明:51Testing软件测试网 BE wJD8C#Ho
function Format(const Format: string; const Args: array of const): string; overload;51Testing软件测试网 xT5DX{2S {
事实上Format方法有两个种形式,另外一种是三个参数的,主要区别在于它是线程安全的,但并不多用,所以这里只对第一个介绍:51Testing软件测试网*B5m]RH |

#M(z9nX(XnV m#m0function Format(const Format: string; const Args: array of const): string; overload;
QT0E;W[B+H G0Format参数是一个格式字符串,用于格式化Args里面的值的。Args又是什么呢,它是一个变体数组,即它里面可以有多个参数,而且每个参数可以不同。51Testing软件测试网A`2eQ)Oyj&[kIOT
如以下例子:51Testing软件测试网$d FM;_f8r
Format('my name is %6s',['wind']);51Testing软件测试网r mH"cX3tp-kA|
返回后就是51Testing软件测试网4p,a"u;_Biv:x7G8vT
my name is wind51Testing软件测试网5g7[0I| Y5\5Chq

!p0Xc0N]BW`0现在来看Format参数的详细情况:
)I6T^Xn&Ww2i9W0Format里面可以写普通的字符串,比如'my name is' 但有些格式指令字符具有特殊意义,比如"%6s"51Testing软件测试网&i&|0KUz J*w

[+@(I DA0格式指令具有以下的形式:
b3u0G\:M&R0"%" [index ":"] ["-"] [width] ["." prec] type
3V T5N0~M0它是以"%"开始,而以type结束,type表示一个具体的类型。中间是用来格式化type类型的指令字符,是可选的。51Testing软件测试网3@#ax-h%l!SJ
51Testing软件测试网`0a1@ HA5d!uD*?
先来看看type,type可以是以下字符:
(mv:Jy[{Md d2fU"g&]k0d 十制数,表示一个整型值51Testing软件测试网]2B6@ rE'{$G
u 和d一样是整型值,但它是无符号的,而如果它对应的值是负的,则返回时是一个2的32次方减去这个绝对值的数51Testing软件测试网0C}'DY)An!PH
   如:Format('this is %u',[-2]);
|4AkU*[PoJ6c1`0   返回的是:this is 429496729451Testing软件测试网:\PI@5o_)hy%j.H
f 对应浮点数51Testing软件测试网s/JU(|@ Pj.F0x
e 科学表示法,对应整型数和浮点数,51Testing软件测试网;{K.Ii;sA$k q$G
   比如Format('this is %e',[-2.22]);51Testing软件测试网QMP ~3h)G
   返回的是:this is -2.22000000000000E+00051Testing软件测试网y7[P3q/rR~0cX
   等一下再说明如果将数的精度缩小
r9K3irs;PE y0g 这个只能对应浮点型,且它会将值中多余的数去掉51Testing软件测试网 r'q$Ew_2Q9[ Kr
   比如Format('this is %g',[02.200]);
/k&llfdj!X/tL0   返回的是:this is 2.2
+IR VFN0n 只能对应浮点型,将值转化为号码的形式。看一个例子就明白了51Testing软件测试网5fab(}$Opq\$jzt
   Format('this is %n',[4552.2176]);51Testing软件测试网;n \9X _.IPt$_ S
   返回的是this is 4,552.2251Testing软件测试网6{.n Y#aa1C rD,I L
   注意有两点,一是只表示到小数后两位,等一下说怎么消除这种情况51Testing软件测试网 q? kE Ol/H+X2^^
   二是,即使小数没有被截断,它也不会也像整数部分一样有逗号来分开的
Zc;XZk-eJw0m 钱币类型,但关于货币类型有更好的格式化方法,这里只是简单的格式化
DT-jIdN-B0`&T0   另外它只对应于浮点值51Testing软件测试网:Z,x6{7@n b n
   Format('this is %m',[9552.21]);51Testing软件测试网+V|)}8p%\+~ U Y
返回:this is ¥9,552.2151Testing软件测试网S)M%\A$U$D
p 对应于指针类型,返回的值是指针的地址,以十六进制的形式来表示51Testing软件测试网(c&pz^ \)se
   例如:
HAm6UuZ(Ym0   var X:integer;51Testing软件测试网8zQ$?^x*xC*r
     p:^integer;51Testing软件测试网8v9au-z$o q:{4vM
   begin
s0}|X`0Y3j0    X:=99;51Testing软件测试网-z vT&q6K"j5p`
    p:=@X;
-Hv/vJ!N~^0    Edit1.Text:=Format('this is %p',[p]);
K}lB B'HG\#i4x0   end;
MN Nn^@}X0   Edit1的内容是:this is 0012F54851Testing软件测试网i9dS2vY([k.{
s 对应字符串类型,不用多说了吧
a bw ]8n0x 必须是一个整形值,以十六进制的形式返回51Testing软件测试网 i'DW z)n;q)t2}
   Edit1.Text:=Format('this is %X',[15]);51Testing软件测试网;w)c#f0Ea)jcD HV
   返回是:this is F
ZL1rB2Q0n"K!_0
m(MgN/I M0类型讲述完毕,下面介绍格式化Type的指令:51Testing软件测试网 Y^K#E8Zj7R
[index ":"] 这个要怎么表达呢,看一个例子
z d$p/w!i0yO'BM0X0             Format('this is %d %d',[12,13]);
nm$Q"{ u3`0             其中第一个%d的索引是0,第二个%d是1,所以字符显示的时候
(R?y(I\z kM0             是这样 this is 12 1351Testing软件测试网G/x]P&Y,Y^g!N@

Q-x,~mS9Qt0             而如果你这样定义:
2NX-[H&k:N"R0             Format('this is %1:d %0:d',[12,13]);51Testing软件测试网uJ(G_)}$i6\
             那么返回的字符串就变成了51Testing软件测试网1n`x-Mn#zX
             this is 13 1251Testing软件测试网q l4Ah` y
             现在明白了吗,[index ":"] 中的index指示Args中参数显示的
cTn sTV?j[0             顺序
4`G%N2k {$G LM0
/izsg"`-} C fs0             还有一种情况,如果这样Format('%d %d %d %0:d %d', [1, 2, 3, 4])
;h GB(LQ[0             将返回1 2 3 1 2。51Testing软件测试网r q| t!?%G uzF7p!_
             如果你想返回的是1 2 3 1 4,必须这样定:
rTS-H]{'\ J0             Format('%d %d %d %0:d %3:d', [1, 2, 3, 4])51Testing软件测试网X2Jd an Z+mk)_
             但用的时候要注意,索引不能超出Args中的个数,不然会引起异常51Testing软件测试网9fV0iO.K$Q
             如Format('this is %2:d %0:d',[12,13]);
.Z+u"ge5M5o pR `m(G0             由于Args中只有12 13 两个数,所以Index只能是0或1,这里为2就错了
EL3h%d:r N%D.]$Tl0[width] 指定将被格式化的值占的宽度,看一个例子就明白了
s|H)o&i2Zup%W0         Format('this is %4d',[12]);
PuqP$Ext3Vx0         输出是:this is    12
'A.a:_D,Rc0         这个是比较容易,不过如果Width的值小于参数的长度,则没有效果。51Testing软件测试网3kY(Id!n:He
         如:Format('this is %1d',[12]);51Testing软件测试网K3cx|m{
         输出是:this is 12
l-]^1k+d&Q r!u:|z0["-"]   这个指定参数向左齐,和[width]合在一起最可以看到效果:51Testing软件测试网kL/|&Wo%|H\
        Format('this is %-4d,yes',[12]);51Testing软件测试网kG1ZSE4s@~(A?d
        输出是:this is 12    ,yes
gF9d(kk%E0       
9P2hH ]K:pF0["." prec] 指定精度,对于浮点数效果最佳:51Testing软件测试网T,~)m vf,^Z
            Format('this is %.2f',['1.1234]);51Testing软件测试网%?-M\U@D8ly?X9A
            输出 this is 1.1251Testing软件测试网S0w#nl6j8NaPP
            Format('this is %.7f',['1.1234]);51Testing软件测试网vN4k'zp)R-n8LqR
            输了 this is 1.123400051Testing软件测试网s*R aR(g

rny2v'iq$y\0            而对于整型数,如果prec比如整型的位数小,则没有效果反之比整形值的位数大,则会在整型值的前面以0补之51Testing软件测试网N eb#@8\B6B
            Format('this is %.7d',[1234]);
jK8}'EpU/z;R{0            输出是:this is 0001234]
d8o(Z.SVZ)Z0         
in^R]/^ O%z0            对于字符型,刚好和整型值相反,如果prec比字符串型的长度大则没有效果,反之比字符串型的长度小,则会截断尾部的字符
Ohhn%z0O;QDO*NUn0            Format('this is %.2s',['1234']);51Testing软件测试网"h H!@.F O4e]
            输出是 this is 12
z9d4I mj5yRSGY0           
&Z,cXe j"wivr6`!G0            而上面说的这个例子:
Be e+i!C"c0            Format('this is %e',[-2.22]);
1v5H^3?"hPuq0            返回的是:this is -2.22000000000000E+000
E e%q+o0P0            怎么去掉多余的0呢,这个就行啦51Testing软件测试网T?|tC3V(AV7ad w
            Format('this is %.2e',[-2.22]);51Testing软件测试网zy.v,tgnvH `t ~

o7ZH2ut Byt0二 FormatDateTime的用法51Testing软件测试网aN5xZ8Qg1V
他的声明为:51Testing软件测试网_q f@/HylN
function FormatDateTime(const Format: string; DateTime: TDateTime): string; overload;51Testing软件测试网b}/\"Y,M |\
当然和Format一样还有一种,但这里只介绍常用的第一种51Testing软件测试网2pym2n g ~
Format参数是一个格式化字符串。DateTime是时间类型。返回值是一种格式化后的字符串51Testing软件测试网ki0o7fi7Xs3ST I
51Testing软件测试网(a[4un2M8o[;kH
重点来看Format参数中的指令字符51Testing软件测试网`J1`7~`t p
c 以短时间格式显示时间,即全部是数字的表示
.TiG/q ovv%K0   FormatdateTime('c',now);51Testing软件测试网B-H2`K/itF\I"?s
   输出为:2004-8-7 9:55:40
A^-U'kkE/v'p0d 对应于时间中的日期,日期是一位则显示一位,两位则显示两位51Testing软件测试网7F D9[ @s*M q
   FormatdateTime('d',now);51Testing软件测试网(F}x;raV8M#J {
   输出可能为1~31
v*_o*{%`V V R0dd 和d的意义一样,但它始终是以两位来显示的51Testing软件测试网)Q^/L(Nb?#`W
   FormatdateTime('dd',now);51Testing软件测试网s4A QK&?B
    输出可能为01~31
8p3{@W Z,D0ddd 显示的是星期几
;O0d7GX;? d0    FormatdateTime('ddd',now);51Testing软件测试网.I:Q5t$C ph#A~
    输出为: 星期六51Testing软件测试网.m+QfM%d T$Y
dddd 和ddd显示的是一样的。
(e#z(S |`eA0    但上面两个如果在其他国家可能不一样。
kGCEsi`0ddddd 以短时间格式显示年月日
.PpdA p(n Kr"G0     FormatdateTime('ddddd',now);
C:{AQ-@KE0     输出为:2004-8-751Testing软件测试网e2nz&zW+W$o k
dddddd 以长时间格式显示年月日
+Cx9vY;T"iII9Xp"eGs0     FormatdateTime('dddddd',now);51Testing软件测试网dU#\$tJ_f
     输出为:2004年8月7日
F(gP{#X0e/ee/eee/eeee 以相应的位数显示年51Testing软件测试网4yx,niH'`'\V
      FormatdateTime('ee',now);
"Y7_4W;tA,k0     输出为:04   (表示04年)
'Z$|G/L H Nu0m/mm/mmm/mmmm 表示月
Q uo:K*}k4J\M6M0      FormatdateTime('m',now);
g&C8^%Z"C'J(nW0      输出为:851Testing软件测试网Wyz a M:q q @F y$D
      FormatdateTime('mm',now);
1CRCb)Ej ?o:A0      输出为   08
P:x c/i7O@CdZ1~0      FormatdateTime('mmm',now);51Testing软件测试网"lVf0R{j
      输出为   八月51Testing软件测试网^ T*l@Ny*c
      FormatdateTime('mmmm',now);51Testing软件测试网TW;xW't1K0i
      输出为   八月
3JQ9az Ej8Q0     和ddd/dddd 一样,在其他国家可能不同
*J!cXgN/u9yB0yy/yyyy 表示年51Testing软件测试网 JwJf*lB.Lnyq
      FormatdateTime('yy',now);51Testing软件测试网 `+R5KFS? o)yV0`
      输出为 04
,_I8D3X3SO,t'D3Bl8G@;A#G0      FormatdateTime('yyyy',now);51Testing软件测试网/^8sqEK
      输出为 2004
1ad'` ^*A&D FH0h/hh,n/nn,s/ss,z/zzz 分别表示小时,分,秒,毫秒
eu-N9Ae_3h+_0t   以短时间格式显示时间
:Q+FS~&r6o dy0      FormatdateTime('t',now);
L LC0X4zV_g0     输出为 10:1751Testing软件测试网_:R)\I,u{
tt 以长时间格式显示时间
k/Q V/_"u F%sf0      FormatdateTime('tt',now);
N F"o't*Ow5a!jM0      输出为10:18:46
&yv}US"o^3Q C!u[h0ampm 以长时间格式显示上午还是下午51Testing软件测试网;n9K @v){#J Y)~lPM|
      FormatdateTime('ttampm',now);51Testing软件测试网1Fj+eh#vS$^+mf t
      输出为:10:22:57上午51Testing软件测试网|s L!M;d1O9Ot!D [a
51Testing软件测试网 B(A7Wh&] \2}6l
大概如此,如果要在Format中加普通的字符串,可以用双引号隔开那些特定义的字符,这样普通字符串中如果含特殊的字符就不会被显示为时间格式啦:51Testing软件测试网x'i4U0Xj\r
FormatdateTime('"today is" c',now);51Testing软件测试网0x6Ih+Ln2b ~UJ
输出为:today is 2004-8-7 10:26:5851Testing软件测试网S,V}H8O#NS
时间中也可以加"-"或"\"来分开日期:51Testing软件测试网@A2~#@3vI F v
FormatdateTime('"today is" yy-mm-dd',now);
7]wd)mp){0IX+b$j0FormatdateTime('"today is" yy\mm\dd',now);51Testing软件测试网^"nFg B*H#Lt
输出为: today is 04-08-0751Testing软件测试网|i/e3h~|:cf?
也可以用":"来分开时间  
Cz,fa W#|0FormatdateTime('"today is" hh:nn:ss',now);
Zf*z g9@0J0输出为:today is 10:32:2351Testing软件测试网o/R'A-^*k]6h!{4`%b;B

~E?a!x%o0三.FormatFloat的用法51Testing软件测试网)Sy?Rw[9f+E

Zff K ?we*E?0常用的声明:
"y.GBg/U+x@0function FormatFloat(const Format: string; Value: Extended): string; overload;
d y%D7~2h.R0和上面一样Format参数为格式化指令字符,Value为Extended类型为什么是这个类型,因为它是所有浮点值中表示范围最大的,如果传入该方法的参数比如Double或者其他,则可以保存不会超出范围。
Z#fz-r(jpz0
]+{.T @.JJ!@)D0关键是看Format参数的用法51Testing软件测试网 aB&\V#e
0   这个指定相应的位数的指令。51Testing软件测试网{e`xA(h1H
    比如:FormatFloat('000.000',22.22);51Testing软件测试网-Cn0h mh%`,zL
    输出的就是022.22051Testing软件测试网-OM h1Y1W2l(Y i
    注意一点,如果整数部分的0的个数小于Value参数中整数的位数,则没有效果
gt?Gl'k)K0    如:FormatFloat('0.00',22.22);51Testing软件测试网;ZUL |9b`"sl
    输出的是:22.22
:}fT {Z7Zu0    但如果小数部分的0小于Value中小数的倍数,则会截去相应的小数和位数
u dg PCP0    如:FormatFloat('0.0',22.22);51Testing软件测试网D JG6Kh)F7P%sI,T;_(w
    输出的是:22.251Testing软件测试网&I4|3H|LT'T
   
0s~I1l g}!P3R0    也可以在整数0中指定逗号,这个整数位数必须大于3个,才会有逗号出现51Testing软件测试网D'kP9lTn jC7C+K
    FormatFloat('0,000.0',2222.22);51Testing软件测试网;B7H5V3b1c$}F
    输出是:2,222.2
]$}7Ufd1e#L*L,}#W0    如果这样FormatFloat('000,0.0',2222.22);51Testing软件测试网{"O-mO9n In^
    它的输出还是:2,222.251Testing软件测试网 Su7bu$O6z
    注意它的规律51Testing软件测试网2^&ponV#s@
51Testing软件测试网8Y:w{+|M(m%Z
#   和0的用法一样,目前我还没有测出有什么不同。
L C2kt1e$Iw8q)t]0    FormatFloat('##.##',22.22);51Testing软件测试网lHW^V/O
    输出是:22.00
:J!j/E;J K.g a0
z:ROR&M5r z%dz5F_OL0R0E   科学表示法,看几个例子大概就明白了
k:M@kh0    FormatFloat('0.00E+00',2222.22);
-e{v |'S0    输出是 2.22E+0351Testing软件测试网V9QjP6y z `"[
    FormatFloat('0000.00E+00',2222.22);51Testing软件测试网)U0R F BzG'j"@
    输出是 2222.22E+0051Testing软件测试网F,OA{:e+C
     FormatFloat('00.0E+0',2222.22);51Testing软件测试网!V]Mz:X'W
    22.2E+2
.B8mu#WV Y%yB^0    明白了吗,全靠E右边的0来支配的。

TAG: Delphi

 

评分:0

我来说两句

Open Toolbar