Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get number of digits in both right, left sides of a decimal number

Tags:

php

math

decimal

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

12345.789 -> RIGHT SIDE LENGTH IS 3 / LEFT SIDE LENGTH IS 5

I know it is readily attainable by helping string functions and exploding the number. I mean is there a mathematically or programmatically way to perform it better than string manipulations.

Your answers would be greatly appreciated.

Update

The best solution for left side till now was:

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

but still no sufficient for right side. Still waiting ...

like image 453
Ali MasudianPour Avatar asked Mar 20 '23 15:03

Ali MasudianPour


2 Answers

To get the digits on the left side you can do this:

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

This uses the base 10 logarithm to get the number of digits.

The right side is harder. A simple approach would look like this, but due to floating point numbers, it would often fail:

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

This will loop through multiplying by 10 until there are no digits past the decimal. That is tested with floor($decimal) != $decimal.

However, as Ali points out, giving it the number 155.11 (a hard to represent digit in binary) results in a answer of 14. This is because as the number is stored as something like 155.11000000000001 with the 32 bits of floating precision we have.

So instead, a more robust solution is needed. (PoPoFibo's solutions above is particularly elegant, and uses PHPs inherit float comparison functions well).

The fact is, we can never distinguish between input of 155.11 and 155.11000000000001. We will never know which number was originally given. They will both be represented the same. However, if we define the number of zeroes that we can see in a row before we just decide the decimal is 'done' than we can come up with a solution:

$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;
    }
} 

This should find the solution given the reasonable assumption that 10 zeroes in a row means any other digits just don't matter.

However, I still like PopoFibo's solution better, as without any multiplication, PHPs default comparison functions effectively do the same thing, without the messiness.

like image 78
Damien Black Avatar answered Apr 07 '23 01:04

Damien Black


I am lost on PHP semantics big time but I guess the following would serve your purpose without the String usage (that is at least how I would do in Java but hopefully cleaner):

Working code here: http://ideone.com/7BnsR3

Non-string solution (only Math)

Left side is resolved hence taking the cue from your question update:

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

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

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

echo($right);

Prints

85

8 for the LHS and 5 for the RHS.

Since I'm taking a floatval that would make 155.0 as 0 RHS which I think is valid and can be resolved by String functions.

like image 22
StoopidDonut Avatar answered Apr 07 '23 00:04

StoopidDonut