VC++中Format函数详解

来源:互联网 发布:ubuntu 网卡配置 编辑:程序博客网 时间:2024/05/17 23:25

一、       VC++Format函数详解

首先看它的声明:

function Format(const Format: string; const Args: array of const): string; overload;

事实上Format方法有两个种形式,另外一种是三个参数的,主要区别在于它是线程安全的,  但并不多用,所以这里只对第一个介绍:

function Format(const Format: string; const Args: array of const): string; overload;

Format参数是一个格式字符串,用于格式化Args里面的值的。

Args又是什么呢,它是一个变体数组,即它里面可以有多个参数,而且每个参数可以不同。

如以下例子:

Format("my name is %6s","wind");

返回后就是

my name is wind

现在来看Format参数的详细情况:

Format里面可以写普通的字符串,比如"my name is"

但有些格式指令字符具有特殊意义,比如"%6s"

格式指令具有以下的形式:

"%" [index ":"] ["-"] [width] ["." prec] type

它是以"%"开始,而以type结束,type表示一个具体的类型。中间是用来

格式化type类型的指令字符,是可选的。

先来看看typetype可以是以下字符:

d十制数,表示一个整型值

ud一样是整型值,但它是无符号的,而如果它对应的值是负的,则返回时是一个232次方减去这个绝对值的数

如:Format("this is %u",-2);

返回的是:this is 4294967294

f对应浮点数

e科学表示法,对应整型数和浮点数,

比如Format("this is %e",-2.22);

返回的是:this is -2.220000E+000

等一下再说明如果将数的精度缩小

g这个只能对应浮点型,且它会将值中多余的数去掉

比如Format("this is %g",02.200);

返回的是:this is 2.2

n只能对应浮点型,将值转化为号码的形式。看一个例子就明白了

Format("this is %n",4552.2176);

返回的是this is 4,552.22

注意有两点,一是只表示到小数后两位,等一下说怎么消除这种情况;二是,即使小数没有被截断,它也不会也像整数部分一样有逗号来分开的

m钱币类型,但关于货币类型有更好的格式化方法,这里只是简单的格式化另外它只对应于浮点值

Format("this is %m",9552.21);

返回:this is9,552.21

p对应于指针类型,返回的值是指针的地址,以十六进制的形式来表示

例如:

Format("this is %p",p);

Edit1的内容是:this is 0012F548

s对应字符串类型,不用多说了吧

x必须是一个整形值,以十六进制的形式返回

Format("this is %X",15);

返回是:this is F

类型讲述完毕,下面介绍格式化Type的指令:

[index ":"]这个要怎么表达呢,看一个例子

Format("this is %d %d",12,13);

其中第一个%d的索引是0,第二个%d1,所以字符显示的时候是这样 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只能是01,这里为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);

输出可能为131

ddd的意义一样,但它始终是以两位来显示的

FormatdateTime('dd',now);

输出可能为0131

ddd显示的是星期几

FormatdateTime('ddd',now);

输出为:星期六

ddddddd显示的是一样的。

但上面两个如果在其他国家可能不一样。

ddddd以短时间格式显示年月日

FormatdateTime('ddddd',now);

输出为:2004-8-7

dddddd以长时间格式显示年月日

FormatdateTime('dddddd',now);

输出为:200487

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" yymmdd',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参数为格式化指令字符,ValueExtended类型为什么是这个类型,因为它是所有浮点值中表示范围最大的,如果传入该方法的参数比如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来支配的

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

ANSI和 UNICODE 的函数对应表

ANSI         UNICODE          通用

(char.h)     (wchar.h)         (tchar.h)

char          wchar_t            TCHAR

char *       wchar_t *         PTCHAR (PTSTR,LPWSTR,PWSTR,WCHAR)

printf        wprintf             _tprintf

scanf        wscanf             _tscanf

atoi           _wtoi               _ttoi

atol           _wtol               _ttol

itoa           _itow               _itot

ltoa           _ltow               _ltot

atof          _wtof               _tstof

strlen        wcslen             _tcslen

strcat        wcscat             _tcscat

strcpy       wcscpy             _tcscpy

strcmp      wcscmp            _tcscmp

 

 

atof, _atof_l, _wtof, _wtof_l

Convert string tofloat

atoi, _atoi_l, _wtoi, _wtoi_l

Convert string toint

_atoi64, _atoi64_l, _wtoi64, _wtoi64_l

Convert string to__int64

atol, _atol_l, _wtol, _wtol_l

Convert string tolong

_ecvt,_ecvt_s

Convertdouble to string of specified length

_fcvt,_fcvt_s

Convertdouble to string with specified number of digits following decimal point

_gcvt,_gcvt_s

Convertdouble number to string; store string in buffer

_itoa, _i64toa, _ui64toa, _itow, _i64tow, _ui64tow,_itoa_s, _i64toa_s, _ui64toa_s, _itow_s, _i64tow_s, _ui64tow_s

Convertint or __int64 to string

_ltoa, _ltow, _ltoa_s, _ltow_s

Convertlong to string

strtod, _strtod_l, wcstod, _wcstod_l

Convert string todouble

strtol, wcstol, _strtol_l, _wcstol_l

Convert string tolong integer

strtoul, _strtoul_l, wcstoul, _wcstoul_l

Convert string tounsigned long integer

_ultoa, _ultow, _ultoa_s, _ultow_s

Convertunsigned long to string

atof, _atof_l, _wtof, _wtof_l

Convert wide-character string to adouble

atoi, _atoi_l, _wtoi, _wtoi_l

Convert wide-character string toint

_atoi64, _atoi64_l, _wtoi64, _wtoi64_l

Convert wide-character string to__int64

atol, _atol_l, _wtol, _wtol_l

Convert wide-character string tolong

 

原创粉丝点击