1. The usage of the Format function
Format is a very common, but it seems annoying method. I try to translate the help of this method so that it has a complete overview for everyone to query:
First look at its declaration:
function Format(const Format: string; const Args: array of const): string; overload;
In fact, the Format method has two forms, and the other has three parameters. The main difference is that it is Thread-safe, but not much use, so only the first one is introduced here:
function Format(const Format: string; const Args: array of const): string; overload;
The Format parameter is a format string used to format the value in Args. What is Args, it is a variant array, that is, it can have multiple parameters in it, and each parameter can be different.
Such as the following example:
Format('my name is %6s',['wind']);
after the return is
my name is wind
Now let's look at the details of the Format parameter:
Format can write ordinary strings, such as 'my name is', but some format command characters have special meanings, such as "%6s"
The format directive has the following form:
"%" [index ":"] ["-"] [width] ["." prec] type
It starts with "%" and ends with type, which indicates a specific type . The middle is the instruction character used to format the type type, which is optional.
Let's first look at type, type can be the following characters:
d decimal number, indicating an integer value
u is an integer value like d, but it is unsigned, and if its corresponding value is negative, it returns When is a 2 to the 32nd power minus the absolute value of the number
such as: Format('this is %u',[-2]);
The return is: this is 4294967294
f corresponds to the floating point number
e scientific notation, corresponds to the integer Type numbers and floating-point numbers,
such as Format('this is %e',[-2.22]);
returns: this is -2.22000000000000E+000
Wait a minute and then explain that if the precision of the number is reduced by
g, this can only correspond to floating-point numbers type, and it will remove the redundant number in the value,
such as Format('this is %g',[02.200]);
The return is: this is 2.2
n can only correspond to floating-point type, and convert the value into the form of a number. Looking at an example, you can understand
Format('this is %n',[4552.2176]);
what is returned is this is 4,552.22
Note that there are two points, one is that it is only expressed to two decimal places, wait a moment to talk about how to eliminate this situation
two Yes, even if the decimal is not truncated, it will not also have a comma to separate the m coin type like the integer part
, but there is a better way to format the currency type, here is just simple formatting
Also it only corresponds to float point value
Format('this is %m',[9552.21]);
Return: this is $9,552.21
p corresponds to the pointer type, the returned value is the address of the pointer, expressed in hexadecimal form, for
example:
var X:integer;
p:^integer;
begin
X:=99;
p:=@X;
Edit1.Text:=Format('this is %p',[p]);
end;
The content of Edit1 is: this is 0012F548
s corresponds to the string Type, needless to say,
x must be an integer value, returned in hexadecimal form
Edit1.Text:=Format('this is %X',[15]); The
return is: this is F
After the type is described, the instructions for formatting Type are described below:
[index ":"] How to express this, see an example
Format('this is %d %d',[12,13]);
the first % The index of d is 0, and the second %d is 1, so the characters are displayed
like this this is 12 13
而如果你这样定义:
Format('this is %1:d %0:d',[12,13]);
那么返回的字符串就变成了
this is 13 12
现在明白了吗,[index ":"] 中的index指示Args中参数显示的
顺序
还有一种情况,如果这样Format('%d %d %d %0:d %d', [1, 2, 3, 4])
将返回1 2 3 1 2。
如果你想返回的是1 2 3 1 4,必须这样定:
Format('%d %d %d %0:d %3:d', [1, 2, 3, 4])
但用的时候要注意,索引不能超出Args中的个数,不然会引起异常
如Format('this is %2:d %0:d',[12,13]);
由于Args中只有12 13 两个数,所以Index只能是0或1,这里为2就错了
[width] 指定将被格式化的值占的宽度,看一个例子就明白了
Format('this is %4d',[12]);
输出是:this is 12
这个是比较容易,不过如果Width的值小于参数的长度,则没有效果。
如:Format('this is %1d',[12]);
输出是:this is 12
["-"] 这个指定参数向左齐,和[width]合在一起最可以看到效果:
Format('this is %-4d,yes',[12]);
输出是:this is 12 ,yes
["." prec] 指定精度,对于浮点数效果最佳:
Format('this is %.2f',['1.1234]);
输出 this is 1.12
Format('this is %.7f',['1.1234]);
输了 this is 1.1234000
而对于整型数,如果prec比如整型的位数小,则没有效果反之比整形值的位数大,则会在整型值的前面以0补之
Format('this is %.7d',[1234]);
输出是:this is 0001234]
对于字符型,刚好和整型值相反,如果prec比字符串型的长度大则没有效果,反之比字符串型的长度小,则会截断尾部的字符
Format('this is %.2s',['1234']);
输出是 this is 12
而上面说的这个例子:
Format('this is %e',[-2.22]);
返回的是:this is -2.22000000000000E+000
怎么去掉多余的0呢,这个就行啦
Format('this is %.2e',[-2.22]);
二 FormatDateTime的用法
他的声明为:
function FormatDateTime(const Format: string; DateTime: TDateTime): string; overload;
当然和Format一样还有一种,但这里只介绍常用的第一种
Format参数是一个格式化字符串。DateTime是时间类型。返回值是一种格式化后的字符串
重点来看Format参数中的指令字符
c 以短时间格式显示时间,即全部是数字的表示
FormatdateTime('c',now);
输出为:2004-8-7 9:55:40
d 对应于时间中的日期,日期是一位则显示一位,两位则显示两位
FormatdateTime('d',now);
输出可能为1~31
dd 和d的意义一样,但它始终是以两位来显示的
FormatdateTime('dd',now);
输出可能为01~31
ddd 显示的是星期几
FormatdateTime('ddd',now);
输出为: 星期六
dddd 和ddd显示的是一样的。
但上面两个如果在其他国家可能不一样。
ddddd 以短时间格式显示年月日
FormatdateTime('ddddd',now);
输出为:2004-8-7
dddddd 以长时间格式显示年月日
FormatdateTime('dddddd',now);
输出为:2004年8月7日
e/ee/eee/eeee 以相应的位数显示年
FormatdateTime('ee',now);
输出为:04 (表示04年)
m/mm/mmm/mmmm 表示月
FormatdateTime('m',now);
输出为:8
FormatdateTime('mm',now);
输出为 08
FormatdateTime('mmm',now);
输出为 八月
FormatdateTime('mmmm',now);
输出为 八月
和ddd/dddd 一样,在其他国家可能不同
yy/yyyy 表示年
FormatdateTime('yy',now);
输出为 04
FormatdateTime('yyyy',now);
输出为 2004
h/hh,n/nn,s/ss,z/zzz 分别表示小时,分,秒,毫秒
t 以短时间格式显示时间
FormatdateTime('t',now);
输出为 10:17
tt 以长时间格式显示时间
FormatdateTime('tt',now);
输出为10:18:46
ampm 以长时间格式显示上午还是下午
FormatdateTime('ttampm',now);
输出为:10:22:57上午
大概如此,如果要在Format中加普通的字符串,可以用双引号隔开那些特定义的字符,这样普通字符串中如果含特殊的字符就不会被显示为时间格式啦:
FormatdateTime('"today is" c',now);
输出为:today is 2004-8-7 10:26:58
时间中也可以加"-"或"/"来分开日期:
FormatdateTime('"today is" yy-mm-dd',now);
FormatdateTime('"today is" yy/mm/dd',now);
输出为: today is 04-08-07
也可以用":"来分开时间
FormatdateTime('"today is" hh:nn:ss',now);
输出为:today is 10:32:23
三.FormatFloat的用法
常用的声明:
function FormatFloat(const Format: string; Value: Extended): string; overload;
和上面一样Format参数为格式化指令字符,Value为Extended类型为什么是这个类型,因为它是所有浮点值中表示范围最大的,如果传入该方法的参数比如Double或者其他,则可以保存不会超出范围。
关键是看Format参数的用法
0 这个指定相应的位数的指令。
比如:FormatFloat('000.000',22.22);
输出的就是022.220
注意一点,如果整数部分的0的个数小于Value参数中整数的位数,则没有效果
如:FormatFloat('0.00',22.22);
输出的是:22.22
但如果小数部分的0小于Value中小数的倍数,则会截去相应的小数和位数
如:FormatFloat('0.0',22.22);
输出的是:22.2
也可以在整数0中指定逗号,这个整数位数必须大于3个,才会有逗号出现
FormatFloat('0,000.0',2222.22);
输出是:2,222.2
如果这样FormatFloat('000,0.0',2222.22);
它的输出还是:2,222.2
注意它的规律
# 和0的用法一样,目前我还没有测出有什么不同。
FormatFloat('##.##',22.22);
输出是:22.00
E 科学表示法,看几个例子大概就明白了
FormatFloat('0.00E+00',2222.22);
输出是 2.22E+03
FormatFloat('0000.00E+00',2222.22);
输出是 2222.22E+00
FormatFloat('00.0E+0',2222.22);
22.2E+2
明白了吗,全靠E右边的0来支配的。