求将个位数格式化为两位字符,如1格式化为'01'的方法

zqc-tutorial 2004-03-19 02:21:27
两位数简单,直接IntToStr就行了,
但是个位数呢?用什么函数呢?
...全文
460 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zqc-tutorial 2004-03-19
  • 打赏
  • 举报
回复
谢谢postren(小虫),谢谢你的提醒,我真是太粗心了,Delphi帮助中关于Format strings有这样一段描述:
d Decimal. The argument must be an integer value. The value is converted to a string of decimal digits. If the format string contains a precision specifier, it indicates that the resulting string must contain at least the specified number of digits; if the value has less digits, the resulting string is left-padded with zeros.
里面就讲到了为整数指定精度其实就是指定了返回字符串的宽度,宽度不足就会在左边补零。
也谢谢 hch_45(HCH ~ahong.net~) ,你给函数比我写的好多了^_^
postren 2004-03-19
  • 打赏
  • 举报
回复
S := Format('%.2d', I);
cll007 2004-03-19
  • 打赏
  • 举报
回复
procedure TForm1.Button2Click(Sender: TObject);
var
i:integer;
begin
i:=4;
caption:=FormatFloat('00',i)
end;
menggirl 2004-03-19
  • 打赏
  • 举报
回复
老大出手了,用吧,没错
hch_45 2004-03-19
  • 打赏
  • 举报
回复
function IntToStr(value, size:integer):string;overload;
var
i : integer;
begin
result := IntToStr(value);
for i := 1 to size - Length(result) do
begin
result := '0' + result;
end;
end;



procedure TForm1.Button2Click(Sender: TObject);
begin
Edit1.Text := IntToStr(4, 2);
end;
zqc-tutorial 2004-03-19
  • 打赏
  • 举报
回复
我现在暂时用的自己写的一个函数:
function MyFunc(N: Integer): string;
begin
Result := IntToStr(N);
if Length(Result) = 1 then
Result := '0' + Result;
end;

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧