PHP - Get length of digits in a number

跟風遠走 提交于 2019-11-29 16:34:06

问题


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. Somehow I can't manage it to work?

Thanks

EDIT: The example code above --^ and its respective method mb_strlen(); works just fine.


回答1:


Maybe:

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



回答2:


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.

$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.

The equation does the logarithm with base 10 then makes the floor of it and adds 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




回答3:


More elegant way :)

ceil(log10($num));



回答4:


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";



回答5:


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.
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.




回答6:


The following function work for either integers or floats (read comments for more info):

/* Counts digits of a number, even floats.
 * $number: The number.
 * $dec: Determines counting digits:
 * 0: Without decimal
 * 1: Only decimal
 * 2: With decimal (i.e. all parts)
 */

// PHP5
function digits_count($number, $dec = 0) {
    $number = abs($number);
    $numberParts = explode(".", $number);
    if (!isset($numberParts[1]))
        $numberParts[1] = 0;
    return ($dec == 1 ? 0 : strlen($numberParts[0])) +
        ($dec == 0 ? 0 : strlen($numberParts[1]));
}

// PHP7
function digits_count($number, int $dec = 0) : int {
    $number = abs($number);
    $numberParts = explode(".", $number);
    return ($dec == 1 ? 0 : strlen($numberParts[0])) +
        ($dec == 0 ? 0 : strlen($numberParts[1] ?? 0));
}

I recommend you using PHP7 one, it's shorter and cleaner.

Hope it helps!




回答7:


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.

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

$number = (int) $stringNum;

and so on...




回答8:


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



回答9:


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;
    }
    ?>`


来源:https://stackoverflow.com/questions/28433798/php-get-length-of-digits-in-a-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!