首頁  >  文章  >  後端開發  >  php自动补齐数字或字符串位数的方法

php自动补齐数字或字符串位数的方法

WBOY
WBOY原創
2016-07-25 08:52:571402瀏覽
  1. //生成4位数,不足前面补0
  2. $var=sprintf("%04d", 2);
  3. echo $var;//结果为0002
  4. echo date('y_m_d', time()).'_'.sprintf('d', rand(0,99));
  5. ?>
复制代码

sprintf()函数 有没有感觉很像c语言 1,语法 sprintf(format,arg1,arg2,arg++) 参数 描述 format 必需。转换格式。 arg1 必需。规定插到 format 字符串中第一个 % 符号处的参数。 arg2 可选。规定插到 format 字符串中第二个 % 符号处的参数。 arg++ 可选。规定插到 format 字符串中第三、四等等 % 符号处的参数。

2,说明 (bbs.it-home.org 脚本学堂 编辑整理)

  1. $number = 123;
  2. $txt = sprintf("%f",$number);
  3. echo $txt;
  4. ?>
复制代码

3,格式数字 number_format()

  1. $number = 1234.56;

  2. // english notation (default)

  3. $english_format_number = number_format($number);
  4. // 1,235
  5. // french notation

  6. $nombre_format_francais = number_format($number, 2, ',', ' ');
  7. // 1 234,56
  8. $number = 1234.5678;

  9. // english notation without thousands seperator

  10. $english_format_number = number_format($number, 2, '.', '');
  11. // 1234.57
  12. ?>
复制代码


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn