Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the complement of less than 10 digits in php

Shulou Source: shulou.com Published: 2022-05-31 18:38:20 09月19日 Update

This article mainly explains "how php achieves less than 10 digits complement". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn "how php achieves less than 10 digits complement" together!

Complement method: 1. Use "str_pad(numerical value, 10,"0",STR_PAD_RIGHT)" statement, less than 10 bits are complemented by zeros;2. Use "sprintf("f", numerical variable)*pow(10,(10-strlen(numerical variable)))" statement, less than 10 bits are complemented by zeros.

Operating environment of this tutorial: Windows 7 system, PHP7.1 version, DELL G3 computer

Php implementation of less than 10 digits 0 complement the method

Use str_pad() function

str_pad() fills the string with a new length

Use str_pad() function to return 10 digits, padding zeros if less than 10 digits.

Note: Possible values for the fourth parameter of this function:

STR_PAD_BOTH -Fill both sides of the string. If not even, the right side gets extra padding.

STR_PAD_LEFT -Left side of padded string.

STR_PAD_RIGHT -The right side of the padded string. This is the default.

2. Use the sprintf() function

Format a numeric value using the sprintf() function, converting it to a floating-point number with specified decimal places.

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

Thank you for reading, the above is "php how to achieve less than 10 digits complement" content, after the study of this article, I believe we have a deeper understanding of how to achieve less than 10 digits complement php this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Digits functions characters strings numeric values learning content variables right decimal methods statements even parameters decimal places that is ideas situations tutorials articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Apple Huawei Redmi MariaDB