Home >Backend Development >PHP Tutorial >php保留2位小数,只舍不入

php保留2位小数,只舍不入

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-20 12:43:571624browse

/**

 * 小数保留后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);

?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn