How to make up for less than 10 digits in php

青灯夜游
Release: 2023-03-15 20:38:02
Original
2169 people have browsed it

Completion method: 1. Use the "str_pad(numeric value, 10, "0", STR_PAD_RIGHT)" statement, and use zeros to supplement less than 10 digits; 2. Use "sprintf(" f", numerical variable)* pow(10,(10-strlen(numeric variable)))" statement, any less than 10 digits are padded with zeros.

How to make up for less than 10 digits in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php implementation is less than 10 Method of padding the digits with 0

1. Use the str_pad() function

str_pad() function to fill the string to the new length

Use the str_pad() function to return 10 digits, and add zeros if it is less than 10 digits.

<?php
echo str_pad(12853,10,"0",STR_PAD_RIGHT)."<br>";
echo str_pad(1252,10,"0",STR_PAD_RIGHT)."<br>";
echo str_pad(1283365,10,"0",STR_PAD_RIGHT)."<br>";
?>
Copy after login

How to make up for less than 10 digits in php

Description: Possible values ​​for the fourth parameter of this function:

  • STR_PAD_BOTH - Fill Both sides of the string. If it's not an even number, the right side gets extra padding.

  • STR_PAD_LEFT - pads the left side of the string.

  • STR_PAD_RIGHT - Pads the right side of the string. This is the default.

2. Use the sprintf() function

  • Use the sprintf() function to format the value and convert it to A floating point number that specifies the number of decimal places.

  • Then multiply the decimal by (10 to the nth power) to convert it to 10 digits

<?php
$c=12345;
echo sprintf("%10f",$c)*pow(10,(10-strlen($c)))."<br>";

$c=123;
echo sprintf("%10f",$c)*pow(10,(10-strlen($c)))."<br>";

$c=123366;
echo sprintf("%10f",$c)*pow(10,(10-strlen($c)))."<br>";

$c=1234452;
echo sprintf("%10f",$c)*pow(10,(10-strlen($c)))."<br>";
?>
Copy after login

How to make up for less than 10 digits in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to make up for less than 10 digits in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!