Home  >  Article  >  Backend Development  >  PHP comes with a function to add 0 or digits before numbers.

PHP comes with a function to add 0 or digits before numbers.

不言
不言Original
2018-04-23 11:33:212507browse

This article mainly introduces PHP's own function to add 0 or pad before numbers. It has a certain reference value. Now I share it with you. Friends in need can refer to it

Many times we need Format the number, for example, if there are insufficient digits, add 0 in front of it. It can be easily implemented with PHP, because PHP comes with related functions.

<?php   
//生成4位数,不足前面补0   
$var=sprintf("%04d", 2);
echo $var;//结果为0002   
echo date(&#39;Y_m_d&#39;, time()).&#39;_&#39;.sprintf(&#39;d&#39;, rand(0,99));
?>

sprintf() function
Does it feel like C language
1. Syntax

sprintf(format,arg1,arg2,arg++)
Parameters 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
The parameter format is the conversion format, starting with the percent sign ("%") and ending with the conversion character. The following possible format values:

  • %% - returns the percent symbol

  • %b - the binary number

  • %c - Character according to ASCII value

  • %d - Signed decimal number

  • %e - Continuous notation (For example, 1.5e 3)

  • ##%u - unsigned decimal number

  • %f - floating point number (local settings aware)

  • %F - floating point number (not local settings aware)

  • %o - octal number

  • % s - string

  • %x - hexadecimal number (lowercase letters)

  • %X - hexadecimal number (uppercase letters) letters)

arg1, arg2, etc. parameters will be inserted into the main string at the percent sign (%) symbol. This function is executed step by step. At the first % sign, arg1 is inserted, at the second % sign, arg2, and so on.

<?php   
$number = 123;   
$txt = sprintf("%f",$number);   
echo $txt;   
?>

3. Format number number_format()

<?php   
$number = 1234.56;

// english notation (default)
$english_format_number = number_format($number);
// 1,235

// French notation
$nombre_format_francais = number_format($number, 2, &#39;,&#39;, &#39; &#39;);
// 1 234,56

$number = 1234.5678;

// english notation without thousands seperator
$english_format_number = number_format($number, 2, &#39;.&#39;, &#39;&#39;);
// 1234.57
?>

Related recommendations:

Complete PHP built-in functions_PHP tutorial

Simple shopping cart function in php’s built-in function



##

The above is the detailed content of PHP comes with a function to add 0 or digits before numbers.. For more information, please follow other related articles on the PHP Chinese website!

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