Delphi 格式化输出 - Format 函数 和 FmtStr 过程

Delphi 格式化函数 Format 函数 和 FmtStr 过程

单元:SysUtils

一、Format 函数

功能:返回按指定方式格式化一个数组常量的字符形式

函数原型:

function Format(const Format: string; const Args: array of const): string;
begin
  FmtStr(Result, Format, Args);
end;

function Format(const Format: string; const Args: array of const;
  const FormatSettings: TFormatSettings): string;
begin
  FmtStr(Result, Format, Args, FormatSettings);
end;

Format参数是一个格式字符串,用于格式化Args里面的值的Args是一个变体数组,即它里面可以有多个参数,而且每个参数可以不同。

如以下例子:

Format('my name is %6s',['wind']);  //返回  my name is wind

Format里面可以写普通的字符串,比如'my name is',但有些格式指令字符具有特殊意义,比如"%6s"格式指令具有以下的形式:

  • "%" [index ":"] ["-"] [width] ["." prec] type
  • "%" [索引 ":"] ["-"] [宽度] ["." 摘要] 类型

它是以"%"开始,而以type结束,type表示一个具体的类型。中间是用来格式化type类型的指令字符,可选。

1、type类型的表示字符:

  • d 十制数,表示一个整型值
  • u 和d一样是整型值,但它是无符号的,如果它对应的值是负数,则返回一个2的32次方减去这个绝对值的数,如:
    • Format('this is %u',[-2]); //返回:this is 4294967294
  • f 对应浮点数
  • e 科学表示法,对应整型数和浮点数,比如:
    • Format('this is %e',[-2.22]);   //返回:this is -2.22000000000000E+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 is ¥9,552.21
  • p 对应于指针类型,返回的值是指针的地址,以十六进制的形式来表示
    • 例如:
    • var
        X:integer;
        p:^integer;
      begin
        X:=99;
        p:=@X;
        Edit1.Text:=Format('this is %p',[p]);    //Edit1返回内容:this is 0012F548
      end;
  • s 对应字符串类型
  • x 必须是一个整形值,以十六进制的形式返回,例如:
    • Edit1.Text:=Format('this is %X',[15]);  //返回:this is F

2、格式化Type的指令: [index ":"]

  Format('this is %d %d',[12,13]);    //this is 12 13      第一个%d的索引是0,第二个%d是1,所以字符显示的时候是这样 this is 12 13
  Format('this is %1:d %0:d',[12,13]);    //返回的字符串就变成  this is 13 12
  Format('%d %d %d %0:d %d', [1, 2, 3, 4])    //返回1 2 3 1 2。
  Format('%d %d %d %0:d %3:d', [1, 2, 3, 4])   //返回的是1 2 3 1 4

注意:索引不能超出Args中的个数,否则,会引起异常,例如:

Format('this is %2:d %0:d',[12,13]);     //由于Args中只有12 13 两个数,所以Index只能是0或1,这里为2就错了   [width] 指定将被格式化的值占的宽度

如果Width的值小于参数的长度,则没有效果,例如:

Format('this is %4d',[12]);   //输出:this is 12
Format('this is %1d',[12]);   //输出:this is 12

3、["-"] 指定参数向左齐,和[width]合在一起最可以看到效果:

Format('this is %-4d,yes',[12]); //输出是:this is 12 ,yes

4、["." prec] 指定精度

4.1 对于浮点数,效果最佳:

Format('this is %.2f',['1.1234]);   //输出 this is 1.12   (保留小数点后2位)
Format('this is %.7f',['1.1234]);   //输出了 this is 1.1234000 (保留小数点后7位,不足填充0)

4.2 对于整型数。如果prec比如整型的位数小,则没有效果反之比整形值的位数大,则会在整型值的前面以 0 补之

Format('this is %.7d',[1234]);   //输出是:this is 0001234 

4.3 对于字符型,刚好和整型值相反,如果prec比字符串型的长度大则没有效果,反之比字符串型的长度小,则会截断尾部的字符

Format('this is %.2s',['1234']);    //输出是 this is 12   {保留两位字符}
Format('this is %e',[-2.22]);       //返回的是:this is -2.22000000000000E+000,
Format('this is %.2e',[-2.22]);     //去掉多余的0

5、其他示例:

Format('x=%d', [12]);      //'x=12' //最普通
Format('x=%3d', [12]);     //'x= 12' //指定宽度
Format('x=%f', [12.0]);     //'x=12.00' //浮点数
Format('x=%.3f', [12.0]);    //'x=12.000' //指定小数
Format('x=%8.2f'[12.0])     // 'x=     12.00' ;
Format('x=%.*f', [5, 12.0]); //'x=12.00000' //动态配置
Format('x=%.5d', [12]);     //'x=00012' //前面补充0
Format('x=%.5x', [12]);      //'x=0000C' //十六进制
Format('x=%1:d%0:d', [12, 13]); //'x=1312' //使用索引
Format('x=%p', [nil]);     //'x=00000000' //指针
Format('x=%1.1e', [12.0]); //'x=1.2E+001' //科学记数法
Format('x=%%', []); //'x=%' //得到"%"
S := Format('%s%d', [S, I]); //S := S + StrToInt(I); //连接字符串

  

  

二、FmtStr 过程 - 第一个参数返回格式化的结果

原型:

procedure FmtStr(var Result: string; const Format: string;
  const Args: array of const);
var
  Len, BufLen: Integer;
  Buffer: array[0..4095] of Char;
begin
  BufLen := SizeOf(Buffer);
  if Length(Format) < (sizeof(Buffer) - (sizeof(Buffer) div 4)) then
    Len := FormatBuf(Buffer, sizeof(Buffer) - 1, Pointer(Format)^, Length(Format), Args)
  else
  begin
    BufLen := Length(Format);
    Len := BufLen;
  end;
  if Len >= BufLen - 1 then
  begin
    while Len >= BufLen - 1 do
    begin
      Inc(BufLen, BufLen);
      Result := '';          // prevent copying of existing data, for speed
      SetLength(Result, BufLen);
      Len := FormatBuf(Pointer(Result)^, BufLen - 1, Pointer(Format)^,
      Length(Format), Args);
    end;
    SetLength(Result, Len);
  end
  else
    SetString(Result, Buffer, Len);
end;

procedure FmtStr(var Result: string; const Format: string;
  const Args: array of const; const FormatSettings: TFormatSettings);
var
  Len, BufLen: Integer;
  Buffer: array[0..4095] of Char;
begin
  BufLen := SizeOf(Buffer);
  if Length(Format) < (sizeof(Buffer) - (sizeof(Buffer) div 4)) then
    Len := FormatBuf(Buffer, sizeof(Buffer) - 1, Pointer(Format)^, Length(Format),
      Args, FormatSettings)
  else
  begin
    BufLen := Length(Format);
    Len := BufLen;
  end;
  if Len >= BufLen - 1 then
  begin
    while Len >= BufLen - 1 do
    begin
      Inc(BufLen, BufLen);
      Result := '';          // prevent copying of existing data, for speed
      SetLength(Result, BufLen);
      Len := FormatBuf(Pointer(Result)^, BufLen - 1, Pointer(Format)^,
        Length(Format), Args, FormatSettings);
    end;
    SetLength(Result, Len);
  end
  else
    SetString(Result, Buffer, Len);
end;

格式指令形式:"%" [index ":"] ["-"] [width] ["." prec] type 以上 Format 函数 都有说明,且Format 函数都是调用FmtStr 实现

FmtStr 示例:

FmtStr(str,'this is %d %d',[12,13]);   //str = this is 12 13        
Format('this is %d %d',[12,13]);       //this is 12 13          
ShowMessage( Format('最大整数: %d', [MaxInt]) );   {最大整数: 2147483647}                  
ShowMessageFmt('最大整数: %d', [MaxInt]); 

创建时间:2019.12.02  更新时间:2020.08.18 11.06

来源:https://www.cnblogs.com/guorongtao/p/11970803.html