PHP精确计算功能示例
本文实例讲述了PHP精确计算功能。分享给大家供大家参考,具体如下:
引言:一定要确保数据的准确性。这是一个好的程序员的基本素养。
<?php
/**
*精确加法
*@param[type]$a[description]
*@param[type]$b[description]
*/
functionmath_add($a,$b,$scale='2'){
returnbcadd($a,$b,$scale);
}
/**
*精确减法
*@param[type]$a[description]
*@param[type]$b[description]
*/
functionmath_sub($a,$b,$scale='2'){
returnbcsub($a,$b,$scale);
}
/**
*精确乘法
*@param[type]$a[description]
*@param[type]$b[description]
*/
functionmath_mul($a,$b,$scale='2'){
returnbcmul($a,$b,$scale);
}
/**
*精确除法
*@param[type]$a[description]
*@param[type]$b[description]
*/
functionmath_div($a,$b,$scale='2'){
returnbcdiv($a,$b,$scale);
}
/**
*精确求余/取模
*@param[type]$a[description]
*@param[type]$b[description]
*/
functionmath_mod($a,$b){
returnbcmod($a,$b);
}
/**
*比较大小
*@param[type]$a[description]
*@param[type]$b[description]
*大于返回1等于返回0小于返回-1
*/
functionmath_comp($a,$b,$scale='5'){
returnbccomp($a,$b,$scale);//比较到小数点位数
}
echomath_add('3.445','3.444')."\n";//加6.88
echomath_sub('3.445','3.444')."\n";//减0.00
echomath_mul('3.445','3.444')."\n";//乘11.86
echomath_div('3.445','3.444')."\n";//除1.00
echomath_mod('3.445','3.444')."\n";//取模0
echomath_comp('3.445','3.444')."\n";//比较1
echomath_add('3.445','3.444','3')."\n";//加6.889
echomath_sub('3.445','3.444','3')."\n";//减0.001
echomath_mul('3.445','3.444','3')."\n";//乘11.864
echomath_div('3.445','3.444','3')."\n";//除1.000
echomath_mod('3.445','3.444')."\n";//取模0
echomath_comp('3.445','3.444')."\n";//比较1
?>
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP数学运算技巧总结》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》、《PHP数组(Array)操作技巧大全》、《PHP常用遍历算法与技巧总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《php正则表达式用法总结》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。