How to get the number of digits in the right, left part of the decimal number

I wonder if there is a good way to get the number of digits in the right / left side of the PHP decimal number. For example:

12345.789→ RIGHT LATERAL LENGTH 3 / LEFT LATERAL LENGTH 5

I know this is easily achievable by helping functions stringand exploding numbers. I mean, there is a mathematical or software way to do this better than string manipulations.

Your answers will be greatly appreciated.

Update

The best solution for the left side so far has been:

$left = floor(log10($x))+1;

but still not enough for the right side. Still waiting ...

+4
source share
5 answers

PHP , , , String ( , Java, , , ):

: http://ideone.com/7BnsR3

( Math)

, :

$value = 12343525.34541;
$left = floor(log10($value))+1;
echo($left);

$num = floatval($value); 
$right = 0;

while($num != round($num, $right)) {
  $right++;
}

echo($right);

85

8 LHS 5 RHS.

floatval, 155.0 0 RHS, , , String.

+2

, :

$left = floor(log10($x))+1;

10, .

. , - :

$decimal = $x - floor($x);
$right = 0;
while (floor($decimal) != $decimal) {
    $right++;
    $decimal *= 10; //will bring in floating point 'noise' over time
}

10, . floor($decimal) != $decimal.

, , 155.11 ( ), 14. , 155.11000000000001 32 .

. ( PoPoFibo PHP, float).

, 155.11 155.11000000000001. , . . , , , , "", :

$x = 155.11; //the number we are testing

$LIMIT = 10; //number of zeroes in a row until we say 'enough'

$right = 0; //number of digits we've checked
$empty = 0; //number of zeroes we've seen in a row

while (floor($x) != $x) {
    $right++;

    $base = floor($x); //so we can see what the next digit is;
    $x *= 10;
    $base *= 10;

    $digit = floor($x) - $base; //the digit we are dealing with

    if ($digit == 0) {
        $empty += 1;
        if ($empty == $LIMIT) {
            $right -= $empty; //don't count all those zeroes
            break; // exit the loop, we're done
        }
    } else {
        $zeros = 0;
    }
} 

, , 10 , .

, - PopoFibo , - PHP - , .

+3
php > $num = 12345.789;
php > $left = strlen(floor($num));
php > $right = strlen($num - floor($num));
php > echo "$left / $right\n";
5 / 16   <--- 16 digits, huh?
php > $parts = explode('.', $num);
php > var_dump($parts);
array(2) {
  [0]=>
  string(5) "12345"
  [1]=>
  string(3) "789"

, - ... "" . , .

+1
$number = 12345.789;

list($whole, $fraction) = sscanf($number, "%d.%d");

, $number , . strlen() . log10() 10, 100, 1000,..., .

// 5 - 3
echo strlen($whole) , " - " , strlen($fraction);

- , . , strlen().

/**
 * Get integer length.
 *
 * @param integer $integer
 *   The integer to count.
 * @param boolean $count_zero [optional]
 *   Whether 0 is to be counted or not, defaults to FALSE.
 * @return integer
 *   The integer length.
 */
function get_int_length($integer, $count_zero = false) {
    // 0 would be 1 in string mode! Highly depends on use case.
    if ($count_zero === false && $integer === 0) {
        return 0;
    }
    return floor(log10(abs($integer))) + 1;
}

// 5 - 3
echo get_int_length($whole) , " - " , get_int_length($fraction);

1 / 3, , .

$number = 1 / 3;
// Above code outputs
// string : 1 - 10
// math   : 0 - 10

$number = bcdiv(1, 3);
// Above code outputs
// string : 1 - 0       <-- oops
// math   : 0 - INF     <-- 8-)

.

+1

.

<?php
$num=12345.789;
$num_str="".$num; // Converting number to string
$array=explode('.',$num_str); //Explode number (String) with .

echo "Left side length : ".intval(strlen($array[0])); // $array[0] contains left hand side then check the string length 
echo "<br>";
if(sizeof($array)>1)
{
echo "Left side length : ".intval(strlen($array[1]));// $array[1] contains left hand  check the string length side
}
?>
0

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


All Articles