How to convert number in billions to a smaller string of comparison?

3

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

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

  • would have to sort: 2,4,8

  • check the range of 2 to 4 = 2, and 4 to 8 = 4

  • The longest range in this case would be 4 , between 4 and 8.

  • Based on this 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, and if in case, would I have input values in this array that would exceed 4 billion, or even larger, because there could be negative numbers, which would double, the interval, as I could 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.

        
    asked by anonymous 09.12.2016 / 17:52

    1 answer

    0

    You must format the number. Let's say that $num is the variable of the number that I want to format:

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

    What is in front of $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!

        
    12.05.2017 / 20:21