Format
2004-11-05 20:18
281 查看
本文摘自《大富翁论坛》blyb
格式化字符串
使用加号(+)操作符和转换函数(如IntToStr),你确实能把已有值组合成字符串,不过另有一种方法能格式化数字、货币值和其他字符串,这就是功能强大的Format函数及其一族。
Format函数参数包括:一个基本文本字符串、一些占位符(通常由%符号标出)和一个数值数组,数组中每个值对应一个占位符。例如,把两个数字格式化为字符串的代码如下:
Format('First%d,Second%d',[n1,n2]);
其中n1和n2是两个整数值,第一个占位符由第一个值替代,第二个占位符由第二个值替代,以此类推。如果占位符输出类型(由%符号后面的字母表示)与对应的参数类型不匹配,将产生一个运行时间错误,因此设置编译时间类型检查会有利于Format函数的使用。
除了%d外,Format函数还定义了许多占位符。这些占位符定义了相应数据类型的默认输出,你可以用更深一层的格式化约束改变默认输出,例如一个宽度约束决定了输出中的字符个数,而精度约束决定了小数点的位数。例如
Format('%8d',[n1]);
该句把数字n1转换成有8个字符的字符串,并通过填充空白使文本右对齐,左对齐用减号(-)。
占位符说明
d(decimal)将整型值转换为十进制数字字符串
x(hexadecimal)将整型值转换为十六进制数字字符串
p(pointer)将指针值转换为十六进制数字字符串
s(string)拷贝字符串、字符、或字符指针值到一个输出字符串
e(exponential)将浮点值转换为指数表示的字符串
f(floatingpoint)将浮点值转换为浮点表示的字符串
g(general)使用浮点或指数将浮点值转换为最短的十进制字符串
n(number)将浮点值转换为带千位分隔符的浮点值
m(money)将浮点值转换为现金数量表示的字符串,转换结果取决于地域设置,详见Delphi帮助文件的Currencyanddate/timeformattingvariables主题
举两例:
This example displays a message on the form's status bar indicating the record count after a record is deleted.
procedure TForm1.MyDataAfterDelete(DataSet: TDataSet);
begin
StatusBar1.SimpleText := Format('There are now %d records in the table', [DataSet.RecordCount]);
end;
This example copies a specified file into the same directory as the (cross-platform) application.
procedure TForm1.Save1Click(Sender: TObject);
var
NewFileName: string;
Msg: string;
NewFile: TFileStream;
OldFile: TFileStream;
begin
NewFileName := ExtractFilePath(Application.ExeName) + ExtractFileName(Edit1.Text);
Msg := Format('Copy %s to %s?', [Edit1.Text, NewFileName]);
if MessageDlg(Msg, mtCustom, mbOKCancel, 0) = mrOK then
begin
OldFile := TFileStream.Create(Edit1.Text, fmOpenRead or fmShareDenyWrite);
try
NewFile := TFileStream.Create(NewFileName, fmCreate or fmShareDenyRead);
try
NewFile.CopyFrom(OldFile, OldFile.Size);
finally
FreeAndNil(NewFile);
end;
finally
FreeAndNil(OldFile);
end;
end;
end;
格式化字符串
使用加号(+)操作符和转换函数(如IntToStr),你确实能把已有值组合成字符串,不过另有一种方法能格式化数字、货币值和其他字符串,这就是功能强大的Format函数及其一族。
Format函数参数包括:一个基本文本字符串、一些占位符(通常由%符号标出)和一个数值数组,数组中每个值对应一个占位符。例如,把两个数字格式化为字符串的代码如下:
Format('First%d,Second%d',[n1,n2]);
其中n1和n2是两个整数值,第一个占位符由第一个值替代,第二个占位符由第二个值替代,以此类推。如果占位符输出类型(由%符号后面的字母表示)与对应的参数类型不匹配,将产生一个运行时间错误,因此设置编译时间类型检查会有利于Format函数的使用。
除了%d外,Format函数还定义了许多占位符。这些占位符定义了相应数据类型的默认输出,你可以用更深一层的格式化约束改变默认输出,例如一个宽度约束决定了输出中的字符个数,而精度约束决定了小数点的位数。例如
Format('%8d',[n1]);
该句把数字n1转换成有8个字符的字符串,并通过填充空白使文本右对齐,左对齐用减号(-)。
占位符说明
d(decimal)将整型值转换为十进制数字字符串
x(hexadecimal)将整型值转换为十六进制数字字符串
p(pointer)将指针值转换为十六进制数字字符串
s(string)拷贝字符串、字符、或字符指针值到一个输出字符串
e(exponential)将浮点值转换为指数表示的字符串
f(floatingpoint)将浮点值转换为浮点表示的字符串
g(general)使用浮点或指数将浮点值转换为最短的十进制字符串
n(number)将浮点值转换为带千位分隔符的浮点值
m(money)将浮点值转换为现金数量表示的字符串,转换结果取决于地域设置,详见Delphi帮助文件的Currencyanddate/timeformattingvariables主题
举两例:
This example displays a message on the form's status bar indicating the record count after a record is deleted.
procedure TForm1.MyDataAfterDelete(DataSet: TDataSet);
begin
StatusBar1.SimpleText := Format('There are now %d records in the table', [DataSet.RecordCount]);
end;
This example copies a specified file into the same directory as the (cross-platform) application.
procedure TForm1.Save1Click(Sender: TObject);
var
NewFileName: string;
Msg: string;
NewFile: TFileStream;
OldFile: TFileStream;
begin
NewFileName := ExtractFilePath(Application.ExeName) + ExtractFileName(Edit1.Text);
Msg := Format('Copy %s to %s?', [Edit1.Text, NewFileName]);
if MessageDlg(Msg, mtCustom, mbOKCancel, 0) = mrOK then
begin
OldFile := TFileStream.Create(Edit1.Text, fmOpenRead or fmShareDenyWrite);
try
NewFile := TFileStream.Create(NewFileName, fmCreate or fmShareDenyRead);
try
NewFile.CopyFrom(OldFile, OldFile.Size);
finally
FreeAndNil(NewFile);
end;
finally
FreeAndNil(OldFile);
end;
end;
end;
相关文章推荐
- BMP文件格式详解(BMP file format)
- protel 99se :format %x invalid or incompatible with argument
- T-sql date/time format samples
- Ubuntu 14.04 RPM debian format
- Dojo 的日期转换函数:dojo.date.loacle.format( value, formatString )
- 1001. A+B Format (2)
- UTF-8, a transformation format of Unicode and ISO 10646
- FindBug:Call to static DateFormat
- System.Globalization.NumberFormatInfo 的 30个属性、1个构造函数和10个方法
- Extjs DateField Bug 当format为年月'Y-m',在当前月(30、31号)选择其他偶数月会乱跳的问题解决方案
- Invalid number format for port number
- @font-face的format属性
- Windows Media Format SDK系统概述
- javascript Date format(js日期格式化)
- Calendar Date ,DateFormat, SimpleDateFormat的区别
- data argument not used by format string
- Qt Creator 2.3.1 产生 “/usr/bin/ld: .: No such file: File format not recognized”
- Java中正则表达式和常用的Math、Random、System、BigInteger、BigDecimal、Date/DateFormat、Calendar类的使用
- HTML Clipboard Format [MSDN资料]
- 页面提交错误,页面间参数传递java.lang.NumberFormatException: null