1.Format 根據指定所需要的格式,格式化字符串。
原型:
function Format(const Format: string; const Args: array of const): string;
例子:
var
s: string;
begin
//指令類型 type
s := Format('最大整數是: %d; 最小整數是: %d',[MaxInt,Low(Integer)]);
//返回: 最大整數是: 2147483647; 最小整數是: -2147483648
{ 提示: 格式指令必須以 % 開始, 不區分大小寫, %d 代表一個整數; 第二個參數是一個變體數組 }
s := Format('最大的無負號整數是: %u',[High(Cardinal)]);
//返回: 最大的無負號整數是: 4294967295
{ %u表示一個無負號整數 }
s := Format('輸入-2的結果是: %u',[-2]);
//返回: 輸入-2的結果是: 4294967294
{ 如果對應 %u 的是個負數, 則返回: 無負號整數最大值 - 這個數的絕對值 + 1 }
s := Format('%s! %s',['你好','我是萬一']);
//返回: 你好! 我是萬一
{ %s 表示字符串 }
s := Format('%f',[Pi]);
//返回: Pi的值是: 3.14
{ %f 表示浮點數, 保留或湊夠兩位小數點 }
s := Format('%g',[01.2345000]);
//返回: 1.2345
{ %g 表示浮點數, 會去掉多餘的 0 }
s := Format('%n',[12345.6789]);
//返回: 12,345.68
{ %n 表示浮點數, 整數部分使用千位分隔符, 保留兩位小數點 }
s := Format('%m',[12345.6789]);
//返回: ¥12,345.68
{ %m 表示浮點數, 加貨幣記號, 轉換結果取決於系統的地域設置 }
s := Format('%e',[12345.6789]);
//返回: 1.23456789000000E+004
{ %e 用科學計數法表示整數或浮點數 }
s := Format('%p',[@Self]);
//返回: 0012F5BC
{ %p 表示指針地址, 用十六進制表示 }
s := Format('%x',[255]);
//返回: FF
{ %x 用十六進制表示一個整數 }
//Index
s := Format('%s%s',['萬','一']);
s := Format('%0:s%1:s',['萬','一']);
//返回: 萬一
{ 上面兩行的結果是一樣的, 0: 對應數組中的第一個值; 1: 對應數組中的第二個值 }
s := Format('%1:s%0:s',['萬','一']);
//返回: 一萬
{ 翻轉了一下順序 }
s := Format('%1:s%0:s%0:s%1:s',['萬','一']);
//返回: 一萬萬一
{ 反覆使用 }
//Width 與對齊方式
s := Format('%d,%8d,%d',[1,2,3]);
//返回: 1, 2,3
{ 給第二個值指定了8個字符的寬度, 缺少的用空格填充; 如果指定少了則無效 }
s := Format('%d,%-8d,%d',[1,2,3]);
//返回: 1,2 ,3
{ - 表示左對齊, 默認是右對齊的; - 符號在 Width 前面、Index 後面 }
//指定精度 prec
s := Format('%.9f',[Pi]);
//返回: 3.141592654
{ 指定小數點的位數, 取值範圍1-9, 輸入0也當1用 }
s := Format('%.5d',[12]);
//返回: 00012
{ 這是給整數指定位數, 如果小了則無效 }
s := Format('%.3s',['Format']);
//返回: For
{ 給字符串指定位數, 如果大了則無效 }
s := Format('%.3e',[123.456]);
//返回: 1.23E+002
{ 給科學計數法指定位數 }
//指令順序:
{ "%" [index ":"] ["-"] [width] ["." prec] type }
ShowMessage(s);
end;
s: string;
begin
//指令類型 type
s := Format('最大整數是: %d; 最小整數是: %d',[MaxInt,Low(Integer)]);
//返回: 最大整數是: 2147483647; 最小整數是: -2147483648
{ 提示: 格式指令必須以 % 開始, 不區分大小寫, %d 代表一個整數; 第二個參數是一個變體數組 }
s := Format('最大的無負號整數是: %u',[High(Cardinal)]);
//返回: 最大的無負號整數是: 4294967295
{ %u表示一個無負號整數 }
s := Format('輸入-2的結果是: %u',[-2]);
//返回: 輸入-2的結果是: 4294967294
{ 如果對應 %u 的是個負數, 則返回: 無負號整數最大值 - 這個數的絕對值 + 1 }
s := Format('%s! %s',['你好','我是萬一']);
//返回: 你好! 我是萬一
{ %s 表示字符串 }
s := Format('%f',[Pi]);
//返回: Pi的值是: 3.14
{ %f 表示浮點數, 保留或湊夠兩位小數點 }
s := Format('%g',[01.2345000]);
//返回: 1.2345
{ %g 表示浮點數, 會去掉多餘的 0 }
s := Format('%n',[12345.6789]);
//返回: 12,345.68
{ %n 表示浮點數, 整數部分使用千位分隔符, 保留兩位小數點 }
s := Format('%m',[12345.6789]);
//返回: ¥12,345.68
{ %m 表示浮點數, 加貨幣記號, 轉換結果取決於系統的地域設置 }
s := Format('%e',[12345.6789]);
//返回: 1.23456789000000E+004
{ %e 用科學計數法表示整數或浮點數 }
s := Format('%p',[@Self]);
//返回: 0012F5BC
{ %p 表示指針地址, 用十六進制表示 }
s := Format('%x',[255]);
//返回: FF
{ %x 用十六進制表示一個整數 }
//Index
s := Format('%s%s',['萬','一']);
s := Format('%0:s%1:s',['萬','一']);
//返回: 萬一
{ 上面兩行的結果是一樣的, 0: 對應數組中的第一個值; 1: 對應數組中的第二個值 }
s := Format('%1:s%0:s',['萬','一']);
//返回: 一萬
{ 翻轉了一下順序 }
s := Format('%1:s%0:s%0:s%1:s',['萬','一']);
//返回: 一萬萬一
{ 反覆使用 }
//Width 與對齊方式
s := Format('%d,%8d,%d',[1,2,3]);
//返回: 1, 2,3
{ 給第二個值指定了8個字符的寬度, 缺少的用空格填充; 如果指定少了則無效 }
s := Format('%d,%-8d,%d',[1,2,3]);
//返回: 1,2 ,3
{ - 表示左對齊, 默認是右對齊的; - 符號在 Width 前面、Index 後面 }
//指定精度 prec
s := Format('%.9f',[Pi]);
//返回: 3.141592654
{ 指定小數點的位數, 取值範圍1-9, 輸入0也當1用 }
s := Format('%.5d',[12]);
//返回: 00012
{ 這是給整數指定位數, 如果小了則無效 }
s := Format('%.3s',['Format']);
//返回: For
{ 給字符串指定位數, 如果大了則無效 }
s := Format('%.3e',[123.456]);
//返回: 1.23E+002
{ 給科學計數法指定位數 }
//指令順序:
{ "%" [index ":"] ["-"] [width] ["." prec] type }
ShowMessage(s);
end;
2.FormatDateTime 格式化日期值
原型:
function FormatDateTime(const Format: string; DateTime: TDateTime): string;
例子:
var
s: string;
begin
//FormatDateTime 的參數1是 String 格式指令, 參數2是 TDateTime 類型的時間
s := FormatDateTime('c', Now); {返回: 2007-12-18 23:56:05}
{指令 c 表示用短格式顯示日期與時間}
s := FormatDateTime('d', Now); {返回: 19}
s := FormatDateTime('d', StrToDateTime('2008-1-1')); {返回: 1}
{d 表示日期}
s := FormatDateTime('dd', Now); {返回: 19}
s := FormatDateTime('dd', StrToDateTime('2008-1-1')); {返回: 01}
{dd 表示雙位日期}
s := FormatDateTime('ddd', Now); {返回: 星期三}
s := FormatDateTime('dddd', Now); {返回: 星期三}
{ddd 與 dddd 表示星期; 可能對不同的語種會有區別}
s := FormatDateTime('ddddd', Now); {返回: 2007-12-19}
{ddddd 五個 d 表示短格式日期}
s := FormatDateTime('dddddd', Now); {返回: 2007年12月19日}
{dddddd 六個 d 表示長格式日期}
s := FormatDateTime('e', Now); {返回: 7}
{e 表示年, 1位}
s := FormatDateTime('ee', Now); {返回: 07}
{ee 表示年, 2位}
s := FormatDateTime('eee', Now); {返回: 2007}
s := FormatDateTime('eeee', Now); {返回: 2007}
{eee 與 eeee 返回4位數年}
s := FormatDateTime('m', Now); {返回: 12}
{m 表示月, 1位}
s := FormatDateTime('mm', StrToDateTime('2008-1-1')); {返回: 01}
{mm 表示月, 2位}
s := FormatDateTime('mmm', Now); {返回: 十二月}
s := FormatDateTime('mmmm', Now); {返回: 十二月}
{mmm 與 mmmm 表示長格式月}
s := FormatDateTime('y', Now); {返回: 07}
s := FormatDateTime('yy', Now); {返回: 07}
s := FormatDateTime('yyy', Now); {返回: 2007}
s := FormatDateTime('yyyy', Now); {返回: 2007}
{y yy yyy yyyy 表示年; 和 e 略有不同}
s := FormatDateTime('t', Now); {返回: 0:21}
s := FormatDateTime('tt', Now); {返回: 0:22:13}
{t tt 表示時間}
s := FormatDateTime('ampm', Now); {返回: 上午}
s := FormatDateTime('tampm', Now); {返回: 0:24 上午}
{ampm 表示上午、下午}
s := FormatDateTime('h', StrToDateTime('2007-12-30 9:58:06')); {返回: 9}
s := FormatDateTime('hh', StrToDateTime('2007-12-30 9:58:06')); {返回: 09}
{h hh 表示時}
s := FormatDateTime('n', StrToDateTime('2007-12-30 9:58:06')); {返回: 58}
s := FormatDateTime('nn', StrToDateTime('2007-12-30 9:58:06')); {返回: 58}
{n nn 表示分}
s := FormatDateTime('s', StrToDateTime('2007-12-30 9:58:06')); {返回: 6}
s := FormatDateTime('ss', StrToDateTime('2007-12-30 9:58:06')); {返回: 06}
{s ss 表示秒}
s := FormatDateTime('z', Now); {返回: 24}
s := FormatDateTime('zz', Now); {返回: 524}
s := FormatDateTime('zzz', Now); {返回: 524}
{z zz zzz 表示毫秒}
s := FormatDateTime('yy/mm/dd', Now); {返回: 07/12/19}
s := FormatDateTime('yy/mm/dd', Now); {返回: 07-12-19}
s := FormatDateTime('yy-mm-dd', Now); {返回: 07-12-19}
s := FormatDateTime('yy*mm*dd', Now); {返回: 07*12*19}
{使用分隔符, - 是默認的, / 是與 - 等效的, 假如我非要用 / 顯示呢?}
s := FormatDateTime('yy"/"mm"/"dd', Now); {返回: 07/12/19}
s := FormatDateTime('"當前時間是: "yyyy-m-d h:n:s:zz', Now);
{返回: 當前時間是: 2007-12-19 0:47:16:576}
{混入的字符串要包含在雙引號中}
ShowMessage(s);
end;
s: string;
begin
//FormatDateTime 的參數1是 String 格式指令, 參數2是 TDateTime 類型的時間
s := FormatDateTime('c', Now); {返回: 2007-12-18 23:56:05}
{指令 c 表示用短格式顯示日期與時間}
s := FormatDateTime('d', Now); {返回: 19}
s := FormatDateTime('d', StrToDateTime('2008-1-1')); {返回: 1}
{d 表示日期}
s := FormatDateTime('dd', Now); {返回: 19}
s := FormatDateTime('dd', StrToDateTime('2008-1-1')); {返回: 01}
{dd 表示雙位日期}
s := FormatDateTime('ddd', Now); {返回: 星期三}
s := FormatDateTime('dddd', Now); {返回: 星期三}
{ddd 與 dddd 表示星期; 可能對不同的語種會有區別}
s := FormatDateTime('ddddd', Now); {返回: 2007-12-19}
{ddddd 五個 d 表示短格式日期}
s := FormatDateTime('dddddd', Now); {返回: 2007年12月19日}
{dddddd 六個 d 表示長格式日期}
s := FormatDateTime('e', Now); {返回: 7}
{e 表示年, 1位}
s := FormatDateTime('ee', Now); {返回: 07}
{ee 表示年, 2位}
s := FormatDateTime('eee', Now); {返回: 2007}
s := FormatDateTime('eeee', Now); {返回: 2007}
{eee 與 eeee 返回4位數年}
s := FormatDateTime('m', Now); {返回: 12}
{m 表示月, 1位}
s := FormatDateTime('mm', StrToDateTime('2008-1-1')); {返回: 01}
{mm 表示月, 2位}
s := FormatDateTime('mmm', Now); {返回: 十二月}
s := FormatDateTime('mmmm', Now); {返回: 十二月}
{mmm 與 mmmm 表示長格式月}
s := FormatDateTime('y', Now); {返回: 07}
s := FormatDateTime('yy', Now); {返回: 07}
s := FormatDateTime('yyy', Now); {返回: 2007}
s := FormatDateTime('yyyy', Now); {返回: 2007}
{y yy yyy yyyy 表示年; 和 e 略有不同}
s := FormatDateTime('t', Now); {返回: 0:21}
s := FormatDateTime('tt', Now); {返回: 0:22:13}
{t tt 表示時間}
s := FormatDateTime('ampm', Now); {返回: 上午}
s := FormatDateTime('tampm', Now); {返回: 0:24 上午}
{ampm 表示上午、下午}
s := FormatDateTime('h', StrToDateTime('2007-12-30 9:58:06')); {返回: 9}
s := FormatDateTime('hh', StrToDateTime('2007-12-30 9:58:06')); {返回: 09}
{h hh 表示時}
s := FormatDateTime('n', StrToDateTime('2007-12-30 9:58:06')); {返回: 58}
s := FormatDateTime('nn', StrToDateTime('2007-12-30 9:58:06')); {返回: 58}
{n nn 表示分}
s := FormatDateTime('s', StrToDateTime('2007-12-30 9:58:06')); {返回: 6}
s := FormatDateTime('ss', StrToDateTime('2007-12-30 9:58:06')); {返回: 06}
{s ss 表示秒}
s := FormatDateTime('z', Now); {返回: 24}
s := FormatDateTime('zz', Now); {返回: 524}
s := FormatDateTime('zzz', Now); {返回: 524}
{z zz zzz 表示毫秒}
s := FormatDateTime('yy/mm/dd', Now); {返回: 07/12/19}
s := FormatDateTime('yy/mm/dd', Now); {返回: 07-12-19}
s := FormatDateTime('yy-mm-dd', Now); {返回: 07-12-19}
s := FormatDateTime('yy*mm*dd', Now); {返回: 07*12*19}
{使用分隔符, - 是默認的, / 是與 - 等效的, 假如我非要用 / 顯示呢?}
s := FormatDateTime('yy"/"mm"/"dd', Now); {返回: 07/12/19}
s := FormatDateTime('"當前時間是: "yyyy-m-d h:n:s:zz', Now);
{返回: 當前時間是: 2007-12-19 0:47:16:576}
{混入的字符串要包含在雙引號中}
ShowMessage(s);
end;
3.FormatFloat 格式化浮點數
原型:
function FormatFloat(const Format: string; Value: Extended): string;
例子:
var
s: string;
begin
//FormatFloat 的參數1是 String 格式指令, 參數2是實數類型 Extended
s := FormatFloat('###.###',12.3456);
//返回: 12.346
s := FormatFloat('000.000',12.3456);
//返回: 012.346
s := FormatFloat('#.###',12.3);
//返回: 12.3
s := FormatFloat('0.000',12.3);
//返回: 12.300
s := FormatFloat('#,#.#',1234567);
//返回: 1,234,567
s := FormatFloat('0,0.0',1234567);
//返回: 1,234,567.0
s := FormatFloat('0.00E+0',1234567);
//返回: 1.23E+6
s := FormatFloat('0.00E+00',1234567);
//返回: 1.23E+06
//在科學計數法中使用 # 好像不合適?
ShowMessage(s);
end;
s: string;
begin
//FormatFloat 的參數1是 String 格式指令, 參數2是實數類型 Extended
s := FormatFloat('###.###',12.3456);
//返回: 12.346
s := FormatFloat('000.000',12.3456);
//返回: 012.346
s := FormatFloat('#.###',12.3);
//返回: 12.3
s := FormatFloat('0.000',12.3);
//返回: 12.300
s := FormatFloat('#,#.#',1234567);
//返回: 1,234,567
s := FormatFloat('0,0.0',1234567);
//返回: 1,234,567.0
s := FormatFloat('0.00E+0',1234567);
//返回: 1.23E+6
s := FormatFloat('0.00E+00',1234567);
//返回: 1.23E+06
//在科學計數法中使用 # 好像不合適?
ShowMessage(s);
end;