• Delphi


    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; 
     
    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; 
    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; 

    Format是一个很常用,却又似乎很烦的方法,本人试图对这个方法的帮助进行一些翻译,让它有一个完整的概貌,以供大家查询之用:

    首先看它的声明:
    function Format(const Format: string; const Args: array of const): string; overload;

    事实上Format方法有两个种形式,另外一种是三个参数的,主要区别在于它是线程安全的,
    但并不多用,所以这里只对第一个介绍:
    function Format(const Format: string; const Args: array of const): string; overload;

    Format参数是一个格式字符串,用于格式化Args里面的值的。Args又是什么呢,
    它是一个变体数组,即它里面可以有多个参数,而且每个参数可以不同。
    如以下例子:

    Format('my name is %6s',['wind']);
    返回后就是my name is wind

    现在来看Format参数的详细情况:
    Format里面可以写普通的字符串,比如'my name is',但有些格式指令字符具有特殊意义,比如"%6s"格式指令具有以下的形式:
    "%" [index ":"] ["-"] [width] ["." prec] type
    它是以"%"开始,而以type结束,type表示一个具体的类型。中间是用来
    格式化type类型的指令字符,是可选的。

    先来看看type,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 对应于指针类型,返回的值是指针的地址,以十六进制的形式来表示
    例如:
    var X:integer;
    p:^integer;
    begin
    X:=99;
    p:=@X;
    Edit1.Text:=Format('this is %p',[p]);
    end;
    Edit1的内容是:this is 0012F548

    s 对应字符串类型,不用多说了吧
    x 必须是一个整形值,以十六进制的形式返回
    Edit1.Text:=Format('this is %X',[15]);
    返回是:this is F

    类型讲述完毕,下面介绍格式化Type的指令:
    [index ":"]这个要怎么表达呢,看一个例子
    Format('this is %d %d',[12,13]);
    其中第一个%d的索引是0,第二个%d是1,所以字符显示的时候是这样 this is 12 13

    而如果你这样定义:
    Format('this is %1:d %0:d',[12,13]);
    那么返回的字符串就变成了this is 13 12。现在明白了吗,[index ":"] 中的index指示Args中参数显示的顺序还有一种情况,如果这样
    Format('%d %d %d %0:d %d', [1, 2, 3, 4])
    将返回1 2 3 1 2。

    如果你想返回的是1 2 3 1 4,必须这样定:
    Format('%d %d %d %0:d %3:d', [1, 2, 3, 4])

    但用的时候要注意,索引不能超出Args中的个数,不然会引起异常如
    Format('this is %2:d %0:d',[12,13]);
    由于Args中只有12 13 两个数,所以Index只能是0或1,这里为2就错了[width] 指定将被格式化的值占的宽度,看一个例子就明白了

    Format('this is %4d',[12]);
    输出是:this is   12,这个是比较容易,不过如果Width的值小于参数的长度,则没有效果。
    如:

    Format('this is %1d',[12]);
    输出是:this is 12

    ["-"]这个指定参数向左齐,和[width]合在一起最可以看到效果:
    Format('this is %-4d,yes',[12]);
    输出是:this is 12   ,yes

    ["." prec] 指定精度,对于浮点数效果最佳:
    Format('this is %.2f',['1.1234]);
    输出 this is 1.12
    Format('this is %.7f',['1.1234]);
    输出了 this is 1.1234000

    而对于整型数,如果prec比如整型的位数小,则没有效果反之比整形值的位数大,则会在整型值的前面以0补之
    Format('this is %.7d',[1234]);
    输出是:this is 0001234]
             
    对于字符型,刚好和整型值相反,如果prec比字符串型的长度大则没有效果,反之比字符串型的长度小,则会截断尾部的字符
    Format('this is %.2s',['1234']);
    输出是 this is 12,而上面说的这个例子:

    Format('this is %e',[-2.22]);
    返回的是:this is -2.22000000000000E+000,怎么去掉多余的0呢,这个就行啦

    Format('this is %.2e',[-2.22]);
        
    参考:

    https://blog.csdn.net/lovescjinbao/article/details/19615263

    https://www.cnblogs.com/mumble/archive/2011/05/25/2056462.html

  • 相关阅读:
    angularJS实现无刷新文件下载
    入门程序,hello world
    rabbitMQ概念详细介绍
    rabbitMQ安装
    truncate与delete的区别
    Java 并发编程:线程间的协作(wait/notify/sleep/yield/join)
    Java多线程编程:Callable、Future和FutureTask浅析
    定时任务 Crontab命令 详解
    使用Spring Session做分布式会话管理
    AngularJS ui-router (嵌套路由)
  • 原文地址:https://www.cnblogs.com/sunylat/p/13948102.html
Copyright © 2020-2023  润新知