Anders Kristoffersson
Anders Kristoffersson

Reputation: 377

Show digits only when needed

I have a code that views a rating of an Item. but.. It shows, ex. '5.0/5' When I want it to view 5/5. But when it is ex. '4.5/5' Then i want it to show the digit.

How do i do it?

Code:

 $rating_sum = intval($votes->rating_sum);
 $rating_count = intval($votes->rating_count);
 $evaluate = ($rating_count==0) ? "0" : number_format($rating_sum/$rating_count,1);

 $output=" Rating: ". $evaluate."/5";
 echo $output;

Upvotes: 0

Views: 90

Answers (1)

Sebass van Boxel
Sebass van Boxel

Reputation: 2602

What about just replacing the '.0' with '' when it's used in $evaluate?

$rating_sum = intval($votes->rating_sum);
$rating_count = intval($votes->rating_count);
$evaluate = ($rating_count==0) ? "0" : number_format($rating_sum/$rating_count,1);

$evaluate = str_replace('.0', '', $evaluate);

$output=" Rating: ". $evaluate."/5";
echo $output;

Upvotes: 4

Related Questions