Delphi格式化函数Format、FormatDateTime和FormatFloat
1.Format 根据指定所需要的格式,格式化字符串。
原型:
function Format(const Format: string; const Args: array of const): string;
例子:
var
s: string;
begin
//指令类型 type
s := Format('最大整数是: %d; 最小整数是: %d',[MaxInt,Low(Integer)]);
//返回: 最大整数是: 2147483647; 最小整数是: -2147483648
s := Format('最大的无负号整数是: %u',[High(Cardinal)]);
//返回: 最大的无负号整数是: 4294967295
s := Format('输入-2的结果是: %u',[-2]);
//返回: 输入-2的结果是: 4294967294
s := Format('%s! %s',['你好','我是万一']);
//返回: 你好! 我是万一
s := Format('%f',[Pi]);
//返回: Pi的值是: 3.14
s := Format('%g',[01.2345000]);
//返回: 1.2345
s := Format('%n',[12345.6789]);
//返回: 12,345.68
s := Format('%m',[12345.6789]);
//返回: ¥12,345.68
s := Format('%e',[12345.6789]);
//返回: 1.23456789000000E+004
s := Format('%p',[@Self]);
//返回: 0012F5BC
s := Format('%x',[255]);
//返回: FF
//Index
s := Format('%s%s',['万','一']);
s := Format('%0:s%1:s',['万','一']);
//返回: 万一
s := Format('%1:s%0:s',['万','一']);
//返回: 一万
s := Format('%1:s%0:s%0:s%1:s',['万','一']);
//返回: 一万万一
//Width 与对齐方式
s := Format('%d,%8d,%d',[1,2,3]);
//返回: 1, 2,3
s := Format('%d,%-8d,%d',[1,2,3]);
//返回: 1,2 ,3
//指定精度 prec
s := Format('%.9f',[Pi]);
//返回: 3.141592654
s := Format('%.5d',[12]);
//返回: 00012
s := Format('%.3s',['Format']);
//返回: For
s := Format('%.3e',[123.456]);
//返回: 1.23E+002
//指令顺序:
{ "%" [index ":"] ["-"] [width] ["." prec] type }
ShowMessage(s);
end;
2.FormatDateTime 格式化日期值
原型:
function FormatDateTime(const Format: string; DateTime: TDateTime): string;
例子:
var
s: string;
begin
//FormatDateTime 的参数1是 String 格式指令, 参数2是 TDateTime 类型的时间
s := FormatDateTime('c', Now); {返回: 2007-12-18 23:56:05}
s := FormatDateTime('d', Now); {返回: 19}
s := FormatDateTime('d', StrToDateTime('2008-1-1')); {返回: 1}
s := FormatDateTime('dd', Now); {返回: 19}
s := FormatDateTime('dd', StrToDateTime('2008-1-1')); {返回: 01}
s := FormatDateTime('ddd', Now); {返回: 星期三}
s := FormatDateTime('dddd', Now); {返回: 星期三}
s := FormatDateTime('ddddd', Now); {返回: 2007-12-19}
s := FormatDateTime('dddddd', Now); {返回: 2007年12月19日}
s := FormatDateTime('e', Now); {返回: 7}
s := FormatDateTime('ee', Now); {返回: 07}
s := FormatDateTime('eee', Now); {返回: 2007}
s := FormatDateTime('eeee', Now); {返回: 2007}
s := FormatDateTime('m', Now); {返回: 12}
s := FormatDateTime('mm', StrToDateTime('2008-1-1')); {返回: 01}
s := FormatDateTime('mmm', Now); {返回: 十二月}
s := FormatDateTime('mmmm', Now); {返回: 十二月}
s := FormatDateTime('y', Now); {返回: 07}
s := FormatDateTime('yy', Now); {返回: 07}
s := FormatDateTime('yyy', Now); {返回: 2007}
s := FormatDateTime('yyyy', Now); {返回: 2007}
s := FormatDateTime('t', Now); {返回: 0:21}
s := FormatDateTime('tt', Now); {返回: 0:22:13}
s := FormatDateTime('ampm', Now); {返回: 上午}
s := FormatDateTime('tampm', Now); {返回: 0:24 上午}
s := FormatDateTime('h', StrToDateTime('2007-12-30 9:58:06')); {返回: 9}
s := FormatDateTime('hh', StrToDateTime('2007-12-30 9:58:06')); {返回: 09}
s := FormatDateTime('n', StrToDateTime('2007-12-30 9:58:06')); {返回: 58}
s := FormatDateTime('nn', StrToDateTime('2007-12-30 9:58:06')); {返回: 58}
s := FormatDateTime('s', StrToDateTime('2007-12-30 9:58:06')); {返回: 6}
s := FormatDateTime('ss', StrToDateTime('2007-12-30 9:58:06')); {返回: 06}
s := FormatDateTime('z', Now); {返回: 24}
s := FormatDateTime('zz', Now); {返回: 524}
s := FormatDateTime('zzz', Now); {返回: 524}
s := FormatDateTime('yy/mm/dd', Now); {返回: 07/12/19}
s := FormatDateTime('yy/mm/dd', Now); {返回: 07-12-19}
s := FormatDateTime('yy-mm-dd', Now); {返回: 07-12-19}
s := FormatDateTime('yymmdd', Now); {返回: 071219}
s := FormatDateTime('yy"/"mm"/"dd', Now); {返回: 07/12/19}
s := FormatDateTime('"当前时间是: "yyyy-m-d h:n:s:zz', Now);
{返回: 当前时间是: 2007-12-19 0:47:16:576}
ShowMessage(s);
end;
3.FormatFloat 格式化浮点数
原型:
function FormatFloat(const Format: string; Value: Extended): string;
例子:
var
s: string;
begin
//FormatFloat 的参数1是 String 格式指令, 参数2是实数类型 Extended
s := FormatFloat('###.###',12.3456);
//返回: 12.346
s := FormatFloat('000.000',12.3456);
//返回: 012.346
s := FormatFloat('#.###',12.3);
//返回: 12.3
s := FormatFloat('0.000',12.3);
//返回: 12.300
s := FormatFloat('#,#.#',1234567);
//返回: 1,234,567
s := FormatFloat('0,0.0',1234567);
//返回: 1,234,567.0
s := FormatFloat('0.00E+0',1234567);
//返回: 1.23E+6
s := FormatFloat('0.00E+00',1234567);
//返回: 1.23E+06
//在科学计数法中使用 # 好像不合适?
ShowMessage(s);
end;