Home  >  Article  >  Backend Development  >  How to automatically complete the number of digits in a number or string in php

How to automatically complete the number of digits in a number or string in php

WBOY
WBOYOriginal
2016-07-25 08:52:571414browse
  1. //Generate 4 digits, add 0 if necessary
  2. $var=sprintf("%04d", 2);
  3. echo $var;//The result is 0002
  4. echo date(' y_m_d', time()).'_'.sprintf('d', rand(0,99));
  5. ?>
Copy code

sprintf() function Does it feel like C language? 1. Grammar sprintf(format,arg1,arg2,arg++) Parameter Description format required. Convert format. arg1 required. Specifies the parameters to be inserted at the first % sign in the format string. arg2 optional. Specifies the parameter to be inserted into the format string at the second % sign. arg++ Optional. Specifies the parameters to be inserted into the format string at the third, fourth, etc. % symbols.

2, Description (edited and organized by bbs.it-home.org Script School)

  1. $number = 123;
  2. $txt = sprintf("%f",$number);
  3. echo $txt;
  4. ?>
Copy code

3, Format number 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. // english notation without thousands seperator

  9. $english_format_number = number_format($number, 2, '.', '');
  10. // 1234.57
  11. ?>

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn