中国星 發表於 2019-12-2 15:26:00

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

<p><span style="font-size: 16px"><strong>Delphi 格式化函数 Format 函数 和 FmtStr 过程</strong></span></p>
<p><span style="font-size: 16px"><strong>单元:SysUtils</strong></span></p>
<p><span style="font-size: 16px"><strong>一、Format 函数</strong></span></p>
<p><span style="font-size: 16px"><strong>功能:返回按指定方式格式化一个数组常量的字符形式</strong></span></p>
<p><span style="font-size: 16px"><strong>函数原型:</strong></span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">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;</span></pre>
</div>
<p><span style="font-size: 16px"><strong>Format参数是一个格式字符串,用于格式化Args里面的值的</strong>。<strong>Args是一个变体数组,即它里面可以有多个参数,而且每个参数可以不同。</strong></span><br><span style="font-size: 16px">如以下例子:</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;">Format('my name is %6s',['wind']);  //返回my name is wind</pre>
</div>
<p><span style="font-size: 16px">Format里面可以写普通的字符串,比如'my name is',但有些格式指令字符具有特殊意义,比如"%6s"格式指令具有以下的形式:</span></p>
<ul>
<li><strong>"%" ["-"] ["." prec] type</strong></li>
<li><span style="font-size: 16px">"%" [索引 ":"] ["-"] [宽度] ["." 摘要] 类型</span></li>
</ul>
<p><span style="font-size: 16px">它是以"%"开始,而以type结束,type表示一个具体的类型。中间是用来格式化type类型的指令字符,可选。</span><br><br></p>
<p><span style="font-size: 16px"><strong>1、type类型的表示字符:</strong></span></p>
<ul>
<li><span style="font-size: 16px"><strong>&nbsp; d</strong> 十制数,表示一个整型值</span></li>
<li><span style="font-size: 16px"><strong>&nbsp; u</strong> 和d一样是整型值,但它是无符号的,如果它对应的值是负数,则返回一个2的32次方减去这个绝对值的数,如:</span>
<ul>
<li><span style="font-size: 16px">Format('this is %u',[-2]);&nbsp; &nbsp; //返回:this is 4294967294</span></li>



</ul>



</li>
<li><span style="font-size: 16px"><strong>&nbsp; f</strong> 对应浮点数</span></li>
<li><span style="font-size: 16px"><strong>&nbsp; e</strong> 科学表示法,对应整型数和浮点数,比如:</span>
<ul>
<li><span style="font-size: 16px">Format('this is %e',[-2.22]);   //返回:this is -2.22000000000000E+000,等一下再说明如果将数的精度缩小</span></li>



</ul>



</li>
<li><span style="font-size: 16px"><strong>&nbsp; g</strong> 这个只能对应浮点型,且它会将值中多余的数去掉,比如</span>
<ul>
<li><span style="font-size: 16px">Format('this is %g',);  //返回:this is 2.2</span></li>



</ul>



</li>
<li><span style="font-size: 16px"><strong>&nbsp; n</strong> 只能对应浮点型,将值转化为号码的形式。</span>
<ul>
<li><span style="font-size: 16px">Format('this is %n',);  //返回的是this is 4,552.22</span></li>
<li><span style="font-size: 16px">&nbsp;注意有两点,一、只表示到小数后两位,&nbsp; 二、即使小数没有被截断,它也不会像整数部分一样有逗号来分开</span></li>



</ul>



</li>
<li><span style="font-size: 16px"><strong>&nbsp; m&nbsp;</strong>钱币类型,但关于货币类型有更好的格式化方法,这里只是简单的格式化,另外它只对应于浮点值</span>
<ul>
<li><span style="font-size: 16px">Format('this is %m',);  //返回:this is ¥9,552.21</span></li>



</ul>



</li>
<li><span style="font-size: 16px"><strong>&nbsp; p</strong> 对应于指针类型,返回的值是指针的地址,以十六进制的形式来表示</span>
<ul>
<li><span style="font-size: 16px">&nbsp;例如:</span></li>
<li>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;">var
X:integer;
p:^integer;
begin
X:=99;
p:=@X;
Edit1.Text:=Format('this is %p',);    //Edit1返回内容:this is 0012F548
end;</pre>
</div>
</li>
</ul>
</li>
</ul>
<ul>
<li><span style="font-size: 16px"><strong>&nbsp; s</strong>&nbsp; 对应字符串类型</span></li>
<li><span style="font-size: 16px"><strong>&nbsp; x</strong>&nbsp; 必须是一个整形值,以十六进制的形式返回,例如:</span>
<ul>
<li><span style="font-size: 16px">&nbsp;Edit1.Text:=Format('this is %X',);  //返回:this is F</span></li>
</ul>
</li>
</ul>
<p>&nbsp;</p>
<p><span style="font-size: 16px"><strong>2、格式化Type的指令:&nbsp;&nbsp;&nbsp;&nbsp;</strong></span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;">Format('this is %d %d',);    //this is 12 13      第一个%d的索引是0,第二个%d是1,所以字符显示的时候是这样 this is 12 13
Format('this is %1:d %0:d',);    //返回的字符串就变成this is 13 12
Format('%d %d %d %0:d %d', )    //返回1 2 3 1 2。
Format('%d %d %d %0:d %3:d', )   //返回的是1 2 3 1 4</pre>
</div>
<p><span style="font-size: 16px">&nbsp;注意:索引不能超出Args中的个数,否则,会引起异常,例如:</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;">Format('this is %2:d %0:d',);   //由于Args中只有12 13 两个数,所以Index只能是0或1,这里为2就错了    指定将被格式化的值占的宽度</pre>
</div>
<p><span style="font-size: 16px">如果Width的值小于参数的长度,则没有效果,例如:</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;">Format('this is %4d',);   //输出:this is 12
Format('this is %1d',);   //输出:this is 12</pre>
</div>
<p>&nbsp;</p>
<p><span style="font-size: 16px"><strong>3、["-"]&nbsp; 指定参数向左齐,</strong>和合在一起最可以看到效果:</span><br><span style="font-size: 16px">&nbsp; Format('this is %-4d,yes',);&nbsp; &nbsp;//输出是:this is 12 ,yes</span></p>
<p>&nbsp;</p>
<p><span style="font-size: 16px"><strong>4、["." prec] 指定精度</strong>,</span></p>
<p><span style="font-size: 16px">&nbsp; 4.1 对于浮点数,效果最佳:</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">Format('this is %.2f',);   //输出 this is 1.12   (保留小数点后2位)
Format('this is %.7f',);   //输出了 this is 1.1234000 (保留小数点后7位,不足填充0)</span></pre>
</div>
<p><span style="font-size: 16px">&nbsp; 4.2 对于整型数。如果prec比如整型的位数小,则没有效果反之比整形值的位数大,则会在整型值的前面以 0 补之</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">Format('this is %.7d',);   //输出是:this is 0001234 </span></pre>
</div>
<p><span style="font-size: 16px">&nbsp; 4.3 对于字符型,刚好和整型值相反,如果prec比字符串型的长度大则没有效果,反之比字符串型的长度小,则会截断尾部的字符</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">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</span></pre>
</div>
<p><strong><span style="font-size: 16px">5、其他示例:</span></strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;">Format('x=%d', );      //'x=12' //最普通
Format('x=%3d', );   //'x= 12' //指定宽度
Format('x=%f', );   //'x=12.00' //浮点数
Format('x=%.3f', );    //'x=12.000' //指定小数
Format('x=%8.2f')   // 'x=   12.00' ;
Format('x=%.*f', ); //'x=12.00000' //动态配置
Format('x=%.5d', );   //'x=00012' //前面补充0
Format('x=%.5x', );      //'x=0000C' //十六进制
Format('x=%1:d%0:d', ); //'x=1312' //使用索引
Format('x=%p', );   //'x=00000000' //指针
Format('x=%1.1e', ); //'x=1.2E+001' //科学记数法
Format('x=%%', []); //'x=%' //得到"%"
S := Format('%s%d', ); //S := S + StrToInt(I); //连接字符串
</pre>
</div>
<p>&nbsp;</p>
<p><span style="color: rgba(0, 0, 0, 1); font-size: 16px"><strong>二、FmtStr 过程 - 第一个参数返回格式化的结果</strong></span></p>
<p><span style="color: rgba(0, 0, 0, 1); font-size: 16px">原型:</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">procedure FmtStr(var Result: string; const Format: string;
const Args: array of const);
var
Len, BufLen: Integer;
Buffer: array of Char;
begin
BufLen := SizeOf(Buffer);
if Length(Format) &lt; (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 &gt;= BufLen - 1 then
begin
    while Len &gt;= 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 of Char;
begin
BufLen := SizeOf(Buffer);
if Length(Format) &lt; (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 &gt;= BufLen - 1 then
begin
    while Len &gt;= 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;</span></pre>
</div>
<p><span style="font-size: 16px">格式指令形式:<strong>"%" ["-"] ["." prec] type&nbsp; </strong>&nbsp;以上 Format 函数 都有说明,且Format 函数都是调用FmtStr 实现</span></p>
<p><span style="font-size: 16px">FmtStr 示例:</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">FmtStr(str,'this is %d %d',);   //str = this is 12 13      
Format('this is %d %d',);       //this is 12 13         
ShowMessage( Format('最大整数: %d', ) );   {最大整数: 2147483647}                  
ShowMessageFmt('最大整数: %d', );
</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><span style="color: rgba(136, 136, 136, 1)">&nbsp;创建时间:2019.12.02  更新时间:2020.08.18、2020.11.06、2022.08.09</span></p>
<p><span style="color: rgba(136, 136, 136, 1)">&nbsp;来源:https://www.cnblogs.com/guorongtao/p/11970803.html</span></p>

</div>
<div id="MySignature" role="contentinfo">
    博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你有所帮助,谢谢!<br><br>
来源:https://www.cnblogs.com/guorongtao/p/11970803.html
頁: [1]
查看完整版本: Delphi 格式化输出 - Format 函数 和 FmtStr 过程