Percentage of 0 to 5

Asked

Viewed 67 times

0

I have an IMDB (Internet Movie Data Base) number, in case the number is 7.7 out of 10. I need to put this number on a scale from 0 to 5 (integers only). On the basis of this reply: https://stackoverflow.com/a/10201086/1844007

I used:

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

Only the results are: 0.385 which is not the actual note of the film(7.7).
What I’m doing wrong?

Complete code corrected with the help of Bonifazio:

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.

  • 1

    Try ( $obj->imdbRating / 2 )

  • You’re right, post your comment as reply I will mark as best response, thank you very much.

  • do not consider as an answer rs, your problem was only in mathematical calculus... I will add a rounding function that works right, then put as an answer

  • I don’t know if I did it right: round($obj->imdbRating / 2,0);

1 answer

2


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

That’s because if you happen to work only with values int, can generate 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
  • Simple answer but it helped me a lot, thank you.

Browser other questions tagged

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