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

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

不言
Release: 2023-03-24 17:14:01
Original
2563 people have browsed it

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));
?>
Copy after login

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

sprintf(format,arg1,arg2,arg++)
Copy after login
Parameters Description
formatRequired. Convert format.
arg1Required. Specifies the parameters to be inserted at the first % sign in the format string.
arg2Optional. 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;   
?>
Copy after login

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
?>
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template