【问题标题】:Show digits only when needed仅在需要时显示数字
【发布时间】:2012-08-10 04:12:03
【问题描述】:

我有一个查看项目评级的代码。但是..它显示,例如。 '5.0/5' 当我希望它查看 5/5 时。但是当它是前。 '4.5/5' 然后我希望它显示数字。

我该怎么做?

代码:

 $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;

【问题讨论】:

    标签: php joomla numbers digits


    【解决方案1】:

    如果在 $evaluate 中使用 '.0' 替换为 '' 怎么样?

    $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;
    

    【讨论】:

    • 如果没有其他考虑(例如,永远不会返回 1.02),那么这是迄今为止最简单的方法。
    猜你喜欢
    • 2021-10-12
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    相关资源
    最近更新 更多