2017-09-05 45 views
-1

我有代碼給我的平均數,這是好的,但是,它給了我一個很長的數字,例如, 4.8571428571429WordPress的 - 平均除以2位小數的位數

是否可以將其降至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; 
?> 

回答

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, '.', ''); 
?> 

希望這會幫助你。謝謝。