PHP Math Conversion Sums

I am trying to convert a cricket over ie 6to show 0.6and 12to show 1.6. I earned everything except the last part, where it returns the full number.

My code is:

foreach($numberofballs as $x){
    $first = floor($x / 6);
    $last = $x - ($first * 6);
    echo $first.'.'.$last;
}

Allows you to designate an array for testing, suppose the array below needs to be converted for this loop

$numberofballs = array(1,2,3,4,5,6);

foreach($numberofballs as $x){
    $first = floor($x / 6);
    $last = $x - ($first * 6);
    echo $first.'.'.$last;
}

/* notes
for 1 it does it right = 0.1
for 2 it does it right = 0.2
for 3 it does it right = 0.3
for 4 it does it right = 0.4
for 5 it does it right = 0.5
how its supposed to work for 6:
for 6 I do not want to get = 1 I would like to get 0.6 and no there is never 0.7
/ end notes */

I am not saying that the code above is incorrect, I just want to get the correct value.

+4
source share
2 answers

Try something like this:

foreach( $numberofballs as $x){


       $first = floor($x / 6);
       $last = $x - ($first * 6);
       if($last==0 && $first>0) {$last=6; $first-=1;}
       echo $first.'.'.$last;
    }
+2
source

, base_convert, .6 .5:

$numberofballs = range(1,24);

foreach( $numberofballs as $x){

    $round = ceil($x/6) - 1;
    echo  base_convert($x + $round, 10, 7)/10;

}

7, .6, , , 1 2 .. ( 0, 1 ..) , .1 .6 .

6, 12, 18 .., , . :

function bowls2overs($ball_number) {

    $round = ceil($ball_number/6) - 1;
    return base_convert($ball_number + $round, 10, 7)/10;

}
+1
source

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


All Articles