How to convert number into billions for a smaller comparison chain?

Asked

Viewed 93 times

3

Imagine you have an exercise in the following aspect, I need to take from an array with random integers, the largest number that is between the intervals.

For example, I have an array: array(2, 8, 4);

  1. would have to order: 2,4,8

  2. check the range 2 to 4 = 2, and 4 to 8 = 4

  3. the longest interval, in this case, would be the 4, between 4 and 8.

Starting from that idea, I have the following code structure:

<?php

class VerifyNumbers
{

    private $arr = array();

    public function __construct(array $arr)
    {
        $this->arr = $arr;
    }

    public function verifyMaxIntersectionFromArray()
    {
        $collection = array();

         sort($this->arr);

        for ($i= 0; $i < count($this->arr); $i++) {
            if (isset($this->arr[$i + 1])) {
               $collection[] = ($this->arr[$i + 1] - $this->arr[$i]);
            }
        } 
        return max($collection);
    }

}

$verifyNumber = new VerifyNumbers(array(100, 3000, 4000, 25, 540, 20, 200, 300));
$maxIntersection = $verifyNumber->verifyMaxIntersectionFromArray();

echo $maxIntersection;

The question I ask is, if in this case, I had input values in this array that were more than 4 billion, or even larger, because there could be negative numbers, which would double, the range, how could I convert these input numbers into a smaller, readable string, because in PHP 5, an invalid digit is passed to octal integer (for example, 8 or 9), the rest of the number will be ignored.

  • This might help Ivan: http://answall.com/questions/9299/integercom-0-%C3%A0-left-%C3%A9-printed-as-other-n%C3%Bamero

  • 1

    http://www.php.net/manual/en/book.bc.php

  • Does your PHP is 32 bits and is breaking the maximum limit of numbers?

  • I found your description a little confusing. "the greater number that is between the intervals. "? From 2 to 4 = 2 ( 2 is less than 4 and not greater, so you refer to 4-2? Or do you want the smallest of them instead of the largest? ) 4 to 8 = 4 (same thing as the previous query)

  • yes, I mean subtraction.

1 answer

0

You should format the number. Let’s imagine that $num is the number variable I want to format:

echo "O seu número formatado é: ". number_format($num, 4);

What’s in front of the $num is how many houses this number will have. So in that case instead of being for example 1.000000 it will be 1.0000!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.