简体   繁体   English

PHP - 获取数字中的数字长度

[英]PHP - Get length of digits in a number

I would like to ask how I can get the length of digits in an Integer.我想问一下如何获得整数中的数字长度。 For example:例如:

$num = 245354;
$numlength = mb_strlen($num);

$numlength should be 6 in this example.在此示例中, $numlength应为 6。 Somehow I can't manage it to work?不知何故我无法管理它工作?

Thanks谢谢

EDIT: The example code above --^ and its respective method mb_strlen();编辑:上面的示例代码 --^ 及其各自的方法mb_strlen(); works just fine.工作得很好。

Maybe:也许:

$num = 245354;
$numlength = strlen((string)$num);

Accepted answer won't work with the big numbers.接受的答案不适用于大数字。 The better way to calculate the length of any number is to invoke floor(log10($num) + 1) with a check for 0 .计算任何数字长度的更好方法是调用floor(log10($num) + 1)并检查0

$num = 12357;
echo $num !== 0 ? floor(log10($num) + 1) : 1; // prints 5

It has multiple advantages.它具有多重优势。 It's faster, you don't do the casting of types, it works on big numbers, it works with different number systems like bin, hex, oct.它更快,你不做类型转换,它适用于大数字,它适用于不同的数字系统,如 bin、hex、oct。

The equation does the logarithm with base 10 then makes the floor of it and adds 1.该方程以 10 为底做对数,然后取其底并加 1。

This solution can work independently on the base, so if you want to calculate the length of binary or hex just change the base of the logarithm.该解决方案可以在基数上独立工作,因此如果要计算二进制或十六进制的长度,只需更改对数的基数。

Working fiddle 工作小提琴

The accepted solution presents a problem when evaluating negative numbers.在评估负数时,公认的解决方案会出现问题。

It works with a positive number:它适用于正数:

$num = 245354;
$numlength = strlen((string)$num);
// Result: 6

But with a negative number, the (-) is added to the count:但如果是负数,则将 (-) 添加到计数中:

$num = -245354;
$numlength = strlen((string)$num);
// Result: 7

Quick workaround:快速解决方法:

$num = -245354;
$numlength = strlen((string)abs($num));
// Result: 6

更优雅的方式:)

ceil(log10($num));

You could also use some basic math!你也可以使用一些基本的数学!

$digits = (int)(log($num,10)+1) 

<?php
  $num = 123;
  $num2 = 1234;
  $num3 = 12345;

  function digits($num){
    return (int) (log($num, 10) + 1);
  }

  echo "\n $num: " . digits($num);  // 123: 3
  echo "\n $num2:" . digits($num2); // 1234: 4
  echo "\n $num3:" . digits($num3); // 12345: 5 
  echo "\n";

Another way to find out the length of a number in digits would be to divide the integer part of the number to 10 until it becomes 0.找出数字长度的另一种方法是将数字的整数部分除以 10,直到它变为 0。

Example:例子:

2021/10 = 202.1 
202/10 = 20.2 
20/10 = 2 
2/10 = 0.2

Code:代码:

function numberGetLength($number) {
     $count = 0;
     while (intval($number) > 0) {
        $number = intval($number) / 10;
        $count += 1;
     }
     return $count
}

Just using some version of (int)(log($num,10)+1) fails for 10, 100, 1000, etc. It counts the number 10 as 1 digit, 100 as two digits, etc. It also fails with 0 or any negative number.仅使用(int)(log($num,10)+1)的某些版本对于 10、100、1000 等会失败。它将数字 10 计为 1 位,将 100 计为两位数等。它也以 0 失败或任何负数。
If you must use math (and the number is non-negative), use:如果您必须使用数学(并且数字为非负数),请使用:
$numlength = (int)(log($num+1, 10)+1);

Or for a math solution that counts the digits in positive OR negative numbers:或者对于以正数或负数计算数字的数学解决方案:
$numlength = ($num>=0) ? (int)(log($num+1, 10)+1) : (int)(log(1-$num, 10)+1);

But the strlen solution is just about as fast in PHP.但是 strlen 解决方案在 PHP 中的速度几乎一样快。

The following function work for either integers or floats (works with PHP7+):以下函数适用于整数或浮点数(适用于 PHP7+):

function digitsCount($number): int
{
    $number = abs($number);
    $numberParts = explode(".", $number);

    return
        strlen($numberParts[0]) +
        (strlen($numberParts[1] ?? 0));
}

In PHP types are loosely set and guessed, if you want to see something as a string if it is an integer , float , and (i have not tried this) bool then @Gorjunav is the most correct answer.在 PHP 中,类型是松散设置和猜测的,如果您想将某些内容视为字符串,如果它是integerfloat和(我没有尝试过) bool那么@Gorjunav 是最正确的答案。

Reset the variable as a string将变量重置为字符串

$stringNum = (string) $num;

Then you can go anything string related you want with it!然后你可以去任何你想要的与它相关的字符串! And vice-versa for changing a string to an int反之亦然,将字符串更改为 int

$number = (int) $stringNum;

and so on...等等...

count only integer value只计算整数值

  `<?php
        $n1 =12345;
        $n2 =123454.55;
        $n3 =12345564.557;
        echo "The Number you Type: ".$n1."<br>";
        $count = 0; 
        while ($n1 != 0)  
        { 
            $n1 = $n1 / 10;
            $n1 = intval($n1);
            ++$count; 
        } 
        echo "The Digit in a Number: ".$count;
    }
    ?>`
echo strlen((string) abs($num)); // using **abs** it'll work with negative integers as well  

Tested in PHP 4.4.9 - 8.0.0在 PHP 4.4.9 - 8.0.0 中测试

$array = array(-1, 0, -0, 1, 4, 9, 10, -10, 20, -20, 100, -100);
foreach( $array as $key => $num ){
    echo $key."\t{$num}\t=>\t".($num !== 0 ? floor(log10(abs($num)) + 1) : 1)."\n";
}
/* Output:
0   -1  =>  1
1   0   =>  1
2   0   =>  1
3   1   =>  1
4   4   =>  1
5   9   =>  1
6   10  =>  2
7   -10 =>  2
8   20  =>  2
9   -20 =>  2
10  100 =>  3
11  -100    =>  3
*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM