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

WBOY
Release: 2016-06-23 13:50:00
Original
1658 people have browsed it

项目中要用到银行家舍入方法,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);    }}
Copy after login


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!