Home  >  Article  >  Backend Development  >  How to determine how many digits a number has in php

How to determine how many digits a number has in php

青灯夜游
青灯夜游Original
2022-02-10 15:44:164713browse

php method to determine how many digits a number is: 1. Use the floor() function to process the specified number and convert it into an integer; 2. Use the strlen() function to count the number of characters contained in the integer. Number, you can calculate the number of digits in the specified number, the syntax is "strlen(floor($num))".

How to determine how many digits a number has in php

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

How to judge a php How many digits does a number have?

In PHP, there are two situations to determine how many digits a number has:

  • The number is an integer, then You can directly use the strlen() function to obtain the number of characters and determine the number of digits.

    strlen() function returns the length of the string. Returns the length of the string if successful, or 0 if the string is empty.

  • The number is a floating point number (including decimals), so you need to round it first (use the floor() function), and then use the strlen() function to get the number of characters.

    The floor() function rounds down to the nearest integer.

Let’s take a look at the implementation code:

<?php
header("Content-type:text/html;charset=utf-8");
$num=120;
echo $num." 是一个 ".strlen(floor($num))." 位数<br>";
$num=120.567;
echo $num." 是一个 ".strlen(floor($num))." 位数";
?>

How to determine how many digits a number has in php

## Recommended learning: "

PHP Video Tutorial

The above is the detailed content of How to determine how many digits a number has in php. 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