zoukankan      html  css  js  c++  java
  • 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|
      ",   1, -1);
      echo sprintf ("|%-4d|%-4d|
      ",   1, -1);
      echo sprintf ("|%+-4d|%+-4d|
      ", 1, -1);
      
      /*
      outputs:
      |  +1|  -1|
      |1   |-1  |
      |+1  |-1  |
      
      */
      
      echo sprintf ("|%04d|
      ",   -2);
      echo sprintf ("|%':4d|
      ",  -2);
      echo sprintf ("|%-':4d|
      ", -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); // binary representation
      printf("%%c = '%c'
      ", $c); // print the ascii character, same as chr() function
      printf("%%d = '%d'
      ", $n); // standard integer representation
      printf("%%e = '%e'
      ", $n); // scientific notation
      printf("%%u = '%u'
      ", $n); // unsigned integer representation of a positive integer
      printf("%%u = '%u'
      ", $u); // unsigned integer representation of a negative integer
      printf("%%f = '%f'
      ", $n); // floating point representation
      printf("%%o = '%o'
      ", $n); // octal representation
      printf("%%s = '%s'
      ", $n); // string representation
      printf("%%x = '%x'
      ", $n); // hexadecimal representation (lower-case)
      printf("%%X = '%X'
      ", $n); // hexadecimal representation (upper-case)
      
      printf("%%+d = '%+d'
      ", $n); // sign specifier on a positive integer
      printf("%%+d = '%+d'
      ", $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'
      View Code


      specifierOutputExample
      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| ",  -2);  //输出:     |-2  |
  • 相关阅读:
    STL中队列queue的常见用法
    牛客网剑指offer第17题——树的子结构
    《剑指offer》网络大神所有习题题解
    牛客网剑指offer第4题——重建二叉树
    牛客网剑指offer第56题——删除链表中重复的节点
    图像处理中的求导问题
    hash_set和hash_map
    hashtable初步——一文初探哈希表
    数据结构-链表的那些事(下)(二)
    数据结构-链表的那些事(上)(一)
  • 原文地址:https://www.cnblogs.com/jade640/p/7100656.html
Copyright © 2011-2022 走看看