如何在PHP中舍入一个数字

hus*_*one -1 php rounding

我运行一个wooocommerce网站,重量是磅,我需要将它们转换为公斤.

我已经从一个网站下载了这个脚本,它完成了这个工作,但是它设置了很多小数的新权重,不知道如何舍入它.

我试图使用,round($new_weight, 2);但它没有采取或不确定在何处放置它或如何.

注意:我对php一无所知.此脚本是页面模板.它会在您打开它时进行更改.

<?php
/*
 *
 * Template name: wconvertor
 */
get_header();

$args = array( 'post_type' => 'product', 'posts_per_page' => 2500  );

$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post(); 
global $product; 

$weight= get_post_meta( get_the_ID(), '_weight', true);

if ( ! empty( $weight ) ) {

    echo "id = ".get_the_ID(). " weight = ".$weight; 

    $new_weight = $weight / 2.2 ; 
    round($new_weight,2);

    echo "<br>new weight = ".$new_weight."  ********************";

    // id , key , value 
    $result = update_post_meta(get_the_ID(), '_weight', $new_weight);
    echo "result = ".$result;

}else{
    echo "<br>NO update for: ".get_the_ID();
}
endwhile; 
wp_reset_query(); 
?>
</div>

<?php get_footer(); ?>
Run Code Online (Sandbox Code Playgroud)

Ali*_*ari 5

阅读这篇文章:

http://php.net/manual/en/function.round.php

例:

<?php
 echo round(1.95583, 2);  // 1.96
 echo round(5.045, 2);    // 5.05
 echo round(5.055, 2);    // 5.06
?>
Run Code Online (Sandbox Code Playgroud)

编辑您的代码:

$new_weight = $weight / 2.2 ; 
$new_weight = round($new_weight,2);
Run Code Online (Sandbox Code Playgroud)