zoukankan      html  css  js  c++  java
  • format 函数详解

    format('% * . * f',[10,4,num]);
    解析: 返回num变量格式化后的字符。整数位为10位,小数后为4位。
    例如:num=1234567890.123456,处理后为“1234567890.1234”的字符串。

    format函数,返回一个指定格式的字符。
    function Format(const Format: string; const Args: array of const): string;
    具体说明:
    const Fromat:string :格式信息
    const Args: 数组。
    主要是格式信息比较麻烦。
    格式化信息主要有以下元素组成:
    "%" + [索引 ":"] + ["-"] + [宽度] + ["." 摘要] + 类型
    格式化信息功能总结如下:

    Format('x=%d', [12]); //'x=12' //最普通
    Format('x=%3d', [12]); //'x= 12' //指定宽度
    Format('x=%f', [12.0]); //'x=12.00' //浮点数
    Format('x=%.3f', [12.0]); //'x=12.000' //指定小数
    Format('x=%8.2f'[12.0]) // 'x= 12.00' ;
    Format('x=%.*f', [5, 12.0]); //'x=12.00000' //动态配置
    Format('x=%.5d', [12]); //'x=00012' //前面补充0
    Format('x=%.5x', [12]); //'x=0000C' //十六进制
    Format('x=%1:d%0:d', [12, 13]); //'x=1312' //使用索引
    Format('x=%p', [nil]); //'x=00000000' //指针
    Format('x=%1.1e', [12.0]); //'x=1.2E+001' //科学记数法
    Format('x=%%', []); //'x=%' //得到"%"
    S := Format('%s%d', [S, I]); //S := S + StrToInt(I); //连接字符串


    格式化信息详解:
    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]);

    //从网上搜的
  • 相关阅读:
    [CTF]zip伪加密
    Node.js躬行记(5)——定时任务的调试
    不一样的资产安全 3D 可视化平台
    冬季里有温度的 3D 可视化智慧供热系统
    公路项目建设可视化进度管理
    ElementUI时间选择控件提交的时间为UTC时间
    Orcal创建触发器
    Orcal常用查询实例集合
    代码优化风格分享
    查某月的天数
  • 原文地址:https://www.cnblogs.com/xspace/p/2916184.html
Copyright © 2011-2022 走看看