Keep the 0 on the left

2

My program ignores me 0 on the left, example pass 001 and take it as 1 and I need everything complete.

My code goes like this

function rotate_to_max($n): int {
    echo "El numero inicial es ".$n."\n";
    $vec = array();

    while($n != 0){
        $vec[] = $n%10;
        $n = (integer)($n/10);

    }
    rsort($vec);
    return implode($vec);

}

And when I get numbers like 000001, I ignore 0 and it's not what I want.

What does my code order me from highest to lowest number passed, then 001 I take it as 1 and I need 001 to take it as 100 equal with another number that pass, example: 00145 = > 54100 and it does is 00145 = > 541, then 541! = 54100

    
asked by Luis Morales 25.11.2016 в 04:05
source

4 answers

1

Do it as a string, example:

function rotate_to_max($n): int {
    echo "El numero inicial es ".$n[0]."\n";
    $vec = array();
    $i = 0;
    while($n != 0){
        $i++;
        $vec[] = $n[$i] % 10;
        $n = (integer)($n[0] /10);

    }
    rsort($vec);
    return implode($vec);

}

Then:

$val = rotate_to_max("0004");

If you are working with floats, this should not be the case.

    
answered by 25.11.2016 / 05:31
source
3

I think you understand that you want to pass integer in your function with 0 leading.

In PHP and I think I know in no other language, the 0 of the left are not stored in memory because they are irrelevant.

Example:

$num = 000001;

echo $num; // salida: 1 (integer)
echo (string)$num; // salida: "1" (string)

However, all leading numbers with a zero are considered octal system .

Octable numbers can only use digits 0-7, just as in the decimal system you can use 0-9 and 0-1 in binary numbers.

Octal numbers:

echo 00;  // 0
echo 01;  // 1
...
echo 06;  // 6
echo 07;  // 7
echo 08;  // Error: no existe
echo 010; // 8
echo 011; // 9
echo 012; // 10

Then to perform your next function rotate_to_max you have to pass the numbers as string and you can solve it as follows:

<?php
// Podemos declarar el modo estricto
declare(strict_types=1);

// Aquí esperamos un string como valor de entrada
function rotate_to_max(string $string) {

    return "\u{202E}$string";
}

echo rotate_to_max('001005'); // ‮001005

+ info on the new features in PHP7

Comrade Dev.Joel has already told you that it could be done with the function strrev() , but since you use the new features of PHP7 you can also invest the string with unicode_escape .

echo "\u{202E}Reversed text"; // outputs ‮Reversed text

View Demo

    
answered by 25.11.2016 в 11:57
2

You have to see the function str_pad

str_pad(1, 3, "0", STR_PAD_LEFT)

The first parameter being "1" the main value, "3" the number of characters and "0" the character with which the 3 positions will be filled and "STR_PAD_LEFT" the position of the characters.

Here is the official documentation: link

    
answered by 25.11.2016 в 06:05
2

Your function reverses a number. but the zeros on the left are lost when you cast (Integer) . I propose a function for this same case making use of a single function in php strrev ($ string) to invest a chain.

function rotate_to_max($n) {
    echo "El número Inicial es :  ".$n."</br>";
    return strrev($n);
}

echo "El número Final es  : " .rotate_to_max("00001");
    
answered by 25.11.2016 в 06:36