php保留2位小数,只舍不入
更新:HHH   时间:2023-1-7


<?php

/**

 * 小数保留后2位,只舍不入

 */

function round_num($num){

if($len = strpos($num,'.')){

$dian_num = substr($num,$len+1,$len+3);//获取小数点后面的数字

if(strlen($dian_num) >= 2){//判断小数点后面的数字长度是否大于2

$new_num = substr($num,0,$len+3);

}else{//补0

$new_num = $num.'0';

}

}else{//补.00

$new_num = $num.'.00';

}

return $new_num;

}

$num = 20.1111;

echo round_num($num);

?>

返回web开发教程...