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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-23 13:50:00
Original
1803 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:
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template