PHP中sprintf、printf等字符串格式化输出中的格式规则总结

sprintf、printf输出格式化字符串。

比如sprintf()的函数原型如下:

string sprintf ( string $format [, mixed $args [, mixed $... ]] )

其中$format用于指定输出的字符串的格式。

进过总结$format遵守以下原型:

%[n$][flags][width][.precision]specifier

其中:

  • n$    是position specifier,指明本占位符代表的是哪个参数

    <?php
    $num = 5;
    $location = ‘tree‘;
    
    $format = ‘The %2$s contains %1$d monkeys‘;
    echo sprintf($format, $num, $location);
  • flags    是一些标志,用来表明是否显示+号、填充字符、对齐方式。具体flags见下表
    flag 描述
    + 默认情况下,只有在数字为负数时,才会显示出符号位‘-’。如果数字为正数,则不显示符号位‘+’。本flag设置之后,不论数字为正或为负,都显示符号位。
    ‘sign 或者 0
    本flag用来设置用来填充的符号。填充符号是为了是输出的字符串达到width指定的长度。默认的填充符是空格。标准的指定填充符的方式是:单引号+填充符,不过对于0作为填充符,即

    可以使用标准定义,也可以直接申明。

    - 本flag用来指明输出的结果是左对齐还是右对齐。缺省下是右对齐,申明本flag后,为左对齐。
    <?php
    echo sprintf ("|%+4d|%+4d|\n",   1, -1);
    echo sprintf ("|%-4d|%-4d|\n",   1, -1);
    echo sprintf ("|%+-4d|%+-4d|\n", 1, -1);
    
    /*
    outputs:
    |  +1|  -1|
    |1   |-1  |
    |+1  |-1  |
    
    */
    
    echo sprintf ("|%04d|\n",   -2);
    echo sprintf ("|%‘:4d|\n",  -2);
    echo sprintf ("|%-‘:4d|\n", -2);
    
    /*
    outputs:
    |-002|
    |::-2|
    |-2::|
    */
  • width    指明本格式输出至少有多少字符。即指明字符的输出长度。见上例。
  • .precision  指明对于浮点数,应该保留几位小数

    <?php
    $money = 123.1234;
    echo sprintf("%.2f", $money);    //123.1
  • specifier  specifier指明应该将参数以何种参数类型对待。

    <?php
    $n =  43951789;
    $u = -43951789;
    $c = 65; // ASCII 65 is ‘A‘
    
    // notice the double %%, this prints a literal ‘%‘ character
    printf("%%b = ‘%b‘\n", $n); // binary representation
    printf("%%c = ‘%c‘\n", $c); // print the ascii character, same as chr() function
    printf("%%d = ‘%d‘\n", $n); // standard integer representation
    printf("%%e = ‘%e‘\n", $n); // scientific notation
    printf("%%u = ‘%u‘\n", $n); // unsigned integer representation of a positive integer
    printf("%%u = ‘%u‘\n", $u); // unsigned integer representation of a negative integer
    printf("%%f = ‘%f‘\n", $n); // floating point representation
    printf("%%o = ‘%o‘\n", $n); // octal representation
    printf("%%s = ‘%s‘\n", $n); // string representation
    printf("%%x = ‘%x‘\n", $n); // hexadecimal representation (lower-case)
    printf("%%X = ‘%X‘\n", $n); // hexadecimal representation (upper-case)
    
    printf("%%+d = ‘%+d‘\n", $n); // sign specifier on a positive integer
    printf("%%+d = ‘%+d‘\n", $u); // sign specifier on a negative integer
    
    %b = ‘10100111101010011010101101‘
    %c = ‘A‘
    %d = ‘43951789‘
    %e = ‘4.39518e+7‘
    %u = ‘43951789‘
    %u = ‘4251015507‘
    %f = ‘43951789.000000‘
    %o = ‘247523255‘
    %s = ‘43951789‘
    %x = ‘29ea6ad‘
    %X = ‘29EA6AD‘
    %+d = ‘+43951789‘
    %+d = ‘-43951789‘

    specifier Output Example
    d  Signed decimal integer 392
    u Unsigned decimal integer 7235
    o Unsigned octal 610
    x Unsigned hexadecimal integer 7fa
    X Unsigned hexadecimal integer (uppercase) 7FA
    f Decimal floating point, lowercase 392.65
    F Decimal floating point, uppercase 392.65
    e Scientific notation (mantissa/exponent), lowercase 3.9265e+2
    E Scientific notation (mantissa/exponent), uppercase 3.9265E+2
    g Use the shortest representation: %e or %f 392.65
    G Use the shortest representation: %E or %F 392.65
    c the argument is treated as an integer, and presented as the character with that ASCII value a
    s String of characters sample
    p the argument is treated as an integer, and presented as a binary number 10100011
    % A % followed by another % character will write a single % to the stream. %

    注明:
    本文主要参考官方文档:http://php.net/manual/en/function.sprintf.php 。并对文档进行了总结,以及对文档部分有偏差部分的纠正。不同处如下:
    1. flags可以是无序的。并不像文档所说——需要按照固定的顺序。flags处的例子可以说明问题。(另外需要指出的是:填充符0和左对齐标志‘-’合用会产生意想不到的结果,见下面的例子)

    echo sprintf ("|%-04d|\n",  -2);  //输出:     |-2  |

时间: 2024-08-02 07:03:21

PHP中sprintf、printf等字符串格式化输出中的格式规则总结的相关文章

[Javascript] 如何自定义字符串格式化输出

在其他语言中十分常见的字符串格式化输出,居然在 Javascript 中不见踪影,于是决定自己实现该方法,以下就是个人编写的最简洁实现: String.prototype.format = function(){ var args = arguments; return this.replace(/\{(\d+)\}/gm, function(ms, p1){return typeof(args[p1]) == 'undefined' ? ms : args[p1]}); } 应用示例: >>

【Shell脚本学习15】shell printf命令:格式化输出语句

printf 命令用于格式化输出, 是echo命令的增强版.它是C语言printf()库函数的一个有限的变形,并且在语法上有些不同. 注意:printf 由 POSIX 标准所定义,移植性要比 echo 好. 如同 echo 命令,printf 命令也可以输出简单的字符串: $printf "Hello, Shell\n" Hello, Shell $ printf 不像 echo 那样会自动换行,必须显式添加换行符(\n). printf 命令的语法: printf format-s

python字符串格式化输出及相关操作代码举例

字符串的格式化 Python 支持格式化字符串的输出 .尽管这样可能会用到非常复杂的表达式,但最基本的用法是将一个值插入到一个有字符串格式符 %s 的字符串中.在   Python 中,字符串格式化使用与 C 中 sprintf 函数一样的语法. 在python中格式化输出字符串通用的形式为: 格式标记字符串 % 要输出的值组 其中,左边部分的"格式标记字符串"可以完全和c中的一致.右边的"值组"如果有两个及以上的值则需要用小括号括起来,中间用逗号隔开. 重点来看左

shell printf命令:格式化输出语句

printf 命令用于格式化输出, 是echo命令的增强版.它是C语言printf()库函数的一个有限的变形,并且在语法上有些不同. 注意:printf 由 POSIX 标准所定义,移植性要比 echo 好. 如同 echo 命令,printf 命令也可以输出简单的字符串: $printf "Hello, Shell\n" Hello, Shell $ printf 不像 echo 那样会自动换行,必须显式添加换行符(\n). printf 命令的语法: printf format-s

字符串格式化输出、while循环、运算符.

1.字符串格式化输出 % 占位符: 声明占位的类型 %s -- 字符串 %d/%i -- 整型 %% 转义 成为普通的% %() 不能多,不能少,一一对应 f"{}" 大括号里的内容一般都放变量 字符串单引号 3.6版本及以上才能使用 2.while 循环 while 关键字 条件: (死循环) 循环体 条件终止循环 break 终止当前的循环 continue 跳出本次循环,继续下次循环 伪装成循环体中最后一行 运算符 算数运算符 / % ** // 赋值运算符 = += -= *=

字符串 格式化输出

name = input("name:")age = int(input("age:"))job = input("job:")salary = input("salary:") if salary.isdigit(): #长的像不像数字,比如200d."200" salary = int(salary)# else:# #print("must input digit")# exit(

python字符串格式化输出

python格式化输出 python格式化输出有两种方式:百分号和format format的功能要比百分号方式强大,其中format独有的可以自定义字符填充空白.字符串居中显示.转换二进制.整数自动分割.百分比显示 等功能是百分号方式不能相比的 1.百分号方式 1.1 格式 %[(name)][flags][width].[precision]typecode 1.2 参数说明 (name) 可选,用于选择指定的key flags 可选,可供选择的值有: + 右对齐:正数前加正好,负数前加负号

Python 学习笔记 -- 字符串格式化输出

1 #如何格式化输出字符串 2 print("{0}是一只{1}".format("我","猫")) 3 print("{a}是一只{b}".format(a="我",b="猫")) 4 print("{0:.1f}{1}".format(3.1415,"GB")) 5 6 #使用%格式化输出 7 print("%c %c %c"

python中while循环运算符及格式化输出

一,while循环 while 条件:       while语句块(循环体) 运行: 判断你给的条件是否为真,如果真则执行循环体.否则跳出循环. 执行完循环体之后再次判断条件是否为真 例子1 我们玩联盟的时候喷打野的的时候,可以用这种方式,这是一个死循环,条件为真,他会一直执行除非手动停止. while True: print("你是不是傻") print("出来帮一下可不可以") print("出来帮一下可不可以") print("