PHP - get the length of digits in number

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

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

$numlength should be 6 in this example. For some reason, I can’t handle it?

thanks

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

+19
source share
7 answers

May be:

 $num = 245354; $numlength = strlen((string)$num); 
+41
source

The accepted answer will not work with large numbers. The best way to calculate the length of any number is to call floor(log10($num) + 1) with a check for 0 .

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

It has several advantages. It is faster, you do not perform type conversion, it works with large numbers, it works with various number systems, such as bin, hex, oct.

The equation makes a logarithm with base 10, then is half and adds 1.

This solution can work regardless of the base, so if you want to calculate the length of the binary or hexadecimal code, just change the base of the logarithm.

Working violin

+8
source

More elegant way :)

 ceil(log10($num)); 
+2
source

You can also use 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"; 
+1
source

Just using some version (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 mathematical solution that counts numbers in positive or negative numbers:
$numlength = ($num>=0) ? (int)(log($num+1, 10)+1) : (int)(log(1-$num, 10)+1);

But strlen's solution is just as fast in PHP.

0
source

The following function works for both integers and floating-point numbers (see comments for details):

 /* Counts digits of a number, even floats. * $number: The number. * $dec: Determines counting digits: * 0: Without decimal * 1: Only decimal * 2: With decimal (ie 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 use PHP7, it is shorter and cleaner.

Hope it helps!

0
source

In PHP, types are freely defined and guessed if you want to see something as a string, if it is an integer, a floating-point number and (I have not tried this) bool, then @Gorjunav is the most correct answer.

Reset a variable as a string

 $stringNum = (string) $num; 

Then you can use any string associated with it! And vice versa to replace the string with int

 $number = (int) $stringNum; 

and so on...

0
source

Source: https://habr.com/ru/post/982269/


All Articles