1

I am using a code to view the TMDB movie rating generally the rating of the movie is 3 characters for example: 7.4 but, many times the rating is only one character like for example 6.

I should add the value .4 or a random number with the dot only if the number is single and doesn't have 3 characters.

3
  • 1
    What have you tried so far? What EXACTLY is the problem? Commented Feb 6, 2021 at 16:02
  • I am using a code on wordpress for the progress bar, the problem is that I need a multiple and not a single number I have to add a random number immediately after the single value, I haven't tried anything yet I don't know where to start Commented Feb 6, 2021 at 16:08
  • So basically you're looking for a way to present 6 as 6.0? Commented Feb 6, 2021 at 20:37

1 Answer 1

1

I would recommend just appending '.0' to the number when it only has a length of 1.

function getRating($value){
  return $value . (strlen($value) === 1 ? '.0': '');
}

But, if you must randomise it:

function getRating($value){
  return $value . (strlen($value) === 1 ? '.'.rand(0,9): '');
}
Sign up to request clarification or add additional context in comments.

2 Comments

im using this code how to ad your function inside? function ProgressBar() { global $post; $vote_average = esc_html(get_post_meta($post->ID,'vote_average', true)); $progress = str_replace(".", "", $vote_average); $count = $progress; if ($count >= 0 && $count < 50) { echo 'danger'; } elseif ($count < 70) { echo ''; } elseif ($count < 80) { echo 'success'; } else { echo 'success'; } }
add it below/outside of your current function and call it inside where you need it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.