Delphi 格式化输出 - Format 函数 和 FmtStr 过程
Delphi 格式化函数 Format 函数 和 FmtStr 过程
单元:SysUtils
一、Format 函数
功能:返回按指定方式格式化一个数组常量的字符形式
函数原型:
1 2 3 4 5 6 7 8 9 10 | 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是一个变体数组,即它里面可以有多个参数,而且每个参数可以不同。
如以下例子:
1 | 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 对应于指针类型,返回的值是指针的地址,以十六进制的形式来表示
- 例如:
-
12345678
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 ":"]
1 2 3 4 | 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中的个数,否则,会引起异常,例如:
1 | Format( 'this is %2:d %0:d' ,[ 12 , 13 ]); //由于Args中只有12 13 两个数,所以Index只能是0或1,这里为2就错了 [width] 指定将被格式化的值占的宽度 |
如果Width的值小于参数的长度,则没有效果,例如:
1 2 | 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 对于浮点数,效果最佳:
1 2 | 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 补之
1 | Format( 'this is %.7d' ,[ 1234 ]); //输出是:this is 0001234 |
4.3 对于字符型,刚好和整型值相反,如果prec比字符串型的长度大则没有效果,反之比字符串型的长度小,则会截断尾部的字符
1 2 3 | 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、其他示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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 过程 - 第一个参数返回格式化的结果
原型:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | 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 示例:
1 2 3 4 | 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、2020.11.06、2022.08.09
来源:https://www.cnblogs.com/guorongtao/p/11970803.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报