Home  >  Article  >  Backend Development  >  php 四舍六入(银行家舍入)实现

php 四舍六入(银行家舍入)实现

WBOY
WBOYOriginal
2016-06-23 13:50:001658browse

项目中要用到银行家舍入方法,php没有现成的方法,所以写了一个

function up6down4($num,$n){		    $pow = pow(10,$n);    $con_a = floor(round($num * $pow * 10,1));    $con_b = floor(round($num * $pow,1));    $con_c = ($num * $pow * 10);    $len = strlen(str_replace('.','',$con_c))-strlen($con_a);	//舍去位为5 && 舍去位后无有效数字 && 舍去位前一位是偶数 ->不进位    if(($con_a % 5 == 0) && bccomp($con_a,$con_c,($len))==0 && ($con_b % 2 ==0) ){        return floor($num * $pow)/$pow;    }else{//四舍五入        return round($num,$n);    }}


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