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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28433798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:51:55  来源:igfitidea点击:

PHP - Get length of digits in a number

phpnumbersstring-length

提问by supersize

I would like to ask how I can get the length of digits in an Integer. For example:

我想问一下如何获得整数中数字的长度。例如:

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

$numlengthshould be 6 in this example. Somehow I can't manage it to work?

$numlength在这个例子中应该是 6。不知何故,我无法让它工作?

Thanks

谢谢

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

编辑:上面的示例代码 --^ 及其各自的方法mb_strlen();工作得很好。

回答by Sergei Gorjunov

Maybe:

也许:

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

回答by Robert

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

工作小提琴

回答by Halayem Anis

More elegant way :)

更优雅的方式:)

ceil(log10($num));

回答by Carlos Vergara

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

回答by Jamie Ciocco

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.

仅使用某些版本的(int)(log($num,10)+1)失败 10、100、1000 等。它将数字 10 计为 1 位,将 100 计为两位数,等等。它也以 0 或任何负数失败。
如果您必须使用数学(并且数字是非负数),请使用:
$numlength = (int)(log($num+1, 10)+1);

或者用于计算正数或负数中的数字的数学解决方案:
$numlength = ($num>=0) ? (int)(log($num+1, 10)+1) : (int)(log(1-$num, 10)+1);

但是 strlen 解决方案在 PHP 中的速度差不多。

回答by MAChitgarha

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.

我建议您使用 PHP7 之一,它更短更干净。

Hope it helps!

希望能帮助到你!

回答by Douglas Richardson

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) boolthen @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...

等等...

回答by Dario Presezzi

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

回答by MD MAMUNAR RASHID

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