Wordpress - 将平均值除以2位小数

时间:2017-09-05 09:12:09

标签: wordpress count

我有代码可以给我一定数量的平均值,但是很好,它给了我很长的数字,例如4.8571428571429

是否可以将其降至2位小数,例如4.85

希望它有意义,我的代码到目前为止

<?php // Get total number of posts in custom post type
    $count_testimonials = wp_count_posts('testimonial');
    $total_testimonials = $count_testimonials->publish;                                            
    $new_average = ($add) / ($total_testimonials);
    echo $new_average;
?>

1 个答案:

答案 0 :(得分:0)

尝试使用此功能:number_format((float)$new_average, 2, '.', '')

<?php
$count_testimonials = wp_count_posts('testimonial');
$total_testimonials = $count_testimonials->publish;                                            
$new_average = ($add) / ($total_testimonials);
$new_average;
echo number_format((float)$new_average, 2, '.', '');
?>

希望这会对你有所帮助。感谢。

相关问题