This article mainly introduced the PHP accurate computation function, combined with the example form analysis PHP carries on the accurate method for subtraction and so on mathematics operation, involves Bcadd, bcsub, Bcmul, bcp and so on function's use skill, needs friend can refer to the next
Introduction: Be sure to ensure the accuracy of your data. This is the basic accomplishment of a good programmer.
<?php/** * Exact addition * @param [type] $a [description] * @param [Type] $b [description] */function math_add ($a, $b, $scale = ' 2 ') {return bcadd ($a, $b, $scale);} /** * Exact subtraction * @param [type] $a [description] * @param [Type] $b [description] */function math_sub ($a, $b, $scale = ' 2 ') {RE Turn bcsub ($a, $b, $scale);} /** * Exact multiplication * @param [type] $a [description] * @param [Type] $b [description] */function Math_mul ($a, $b, $scale = ' 2 ') {RE Turn Bcmul ($a, $b, $scale);} /** * Exact Division * @param [type] $a [description] * @param [Type] $b [description] */function math_p ($a, $b, $scale = ' 2 ') {Retu RN bcp ($a, $b, $scale);} /** * Accurate redundancy/modulus * @param [type] $a [description] * @param [Type] $b [description] */function math_mod ($a, $b) {return bcmod ($a, $b);} /** * Compare size * @param [type] $a [description] * @param [Type] $b [description] * greater than return 1 equals return 0 less than return-1 */function math_comp ($a, $b, $scale = ' 5 ') {return Bccomp ($a, $b, $scale);//Compare to the number of decimal places}echo math_add (' 3.445 ', ' 3.444 '). " \ n "; Add 6.88echo math_sub (' 3.445 ', ' 3.444 '). " \ n "; //Minus 0.00echo Math_mul (' 3.445 ', ' 3.444 '). " \ n "; Multiply 11.86echo math_p (' 3.445 ', ' 3.444 '). " \ n "; Except 1.00echo math_mod (' 3.445 ', ' 3.444 '). " \ n "; Modulo 0echo math_comp (' 3.445 ', ' 3.444 '). " \ n ";//Compare 1echo Math_add (' 3.445 ', ' 3.444 ', ' 3 ')." \ n "; Add 6.889echo math_sub (' 3.445 ', ' 3.444 ', ' 3 '). " \ n "; Minus 0.001echo Math_mul (' 3.445 ', ' 3.444 ', ' 3 '). " \ n "; Multiply 11.864echo math_p (' 3.445 ', ' 3.444 ', ' 3 '). " \ n "; Except 1.000echo math_mod (' 3.445 ', ' 3.444 '). " \ n "; Modulo 0echo math_comp (' 3.445 ', ' 3.444 '). " \ n ";//Compare 1?>
The above is the whole content of this article, I hope that everyone's study has helped.