Percentage from 0 to 5

0

I have an IMDB (Internet Movie Data Base) number, in which case the number is 7.7 out of 10. I need to put this number on a scale of 0 to 5 (integers only). Based on this response: link

I used:

$res = ($obj->imdbRating / 100) * 5;

But the result gives: 0.385 which is not the actual note of the movie (7.7).
What am I doing wrong?

Fixed code with the help of marcelo-bonifazio : / p>

function imdb($filme) {
  $url = 'http://www.omdbapi.com/?i=&t=' . strtolower(str_replace(' ', '+', $filme));
  $json = file_get_contents($url);
  $obj = json_decode($json);
  $res = round($obj->imdbRating / 2,0);
  return $res;
}

Thank you very much.

    
asked by anonymous 17.08.2016 / 02:16

1 answer

2

Your problem is mainly in the calculation, if you are working with integer values, I recommend using the round function together with the intval function.

This is because if you happen to only work with int values, it may cause some conflict.

$obj = 7.7;
$new_value = $obj / 2;
// Valor real = 3.8
echo round( $new_value ).'</br>';
echo gettype ( round( $new_value )  ).'</br>';
// Imprime 4 e tipo double
echo intval ( $new_value ).'</br>';
echo gettype ( intval ( $new_value )  ).'</br>';
// Imprime 3 e tipo integer
echo intval ( round( $new_value ) ).'</br>';
echo gettype ( intval ( round( $new_value ) )  ).'</br>';
// Imprime 4 e tipo integer
    
17.08.2016 / 02:32