The example in this article describes how PHP uses extremely long (extra large) number operations to prevent numbers from being displayed in scientific notation. Share it with everyone for your reference. The details are as follows:
PHP will make errors when calculating large numerical operations. When the number is too large, the value will become scientific notation. So how to perform PHP extremely large numerical operations, including addition, subtraction, multiplication, division, and power operations? , square root, modulo operation?
To solve the problem of scientific notation, just add a pair of quotation marks when assigning the value.
For example:
<?php $n = '22222222222222222222222222220'; echo $n; ?>
If you don’t add quotation marks, it will display 2.2222222222222E+28, after adding quotation marks Displaying 2222222222222222222222222220
Extremely large numerical operations, including addition, subtraction, multiplication, division, power operations, square roots, and modulo operations.
Use PHP’s bcmath function to create a custom function, the code is as follows,
<?php function calc($m,$n,$x){ $errors=array( '被除数不能为零', '负数没有平方根' ); switch($x){ case 'add': $t=bcadd($m,$n); break; case 'sub': $t=bcsub($m,$n); break; case 'mul': $t=bcmul($m,$n); break; case 'div': if($n!=0){ $t=bcdiv($m,$n); }else{ return $errors[0]; } break; case 'pow': $t=bcpow($m,$n); break; case 'mod': if($n!=0){ $t=bcmod($m,$n); }else{ return $errors[0]; } break; case 'sqrt': if($m>=0){ $t=bcsqrt($m); }else{ return $errors[1]; } break; } $t=preg_replace("/\..*0+$/",'',$t); return $t; } /*用法举例*/ echo calc('11111111111111111111111111111111110','10','add'); ?>
Usage:
calc( Parameter 1, parameter 2, parameter 3);
Parameter 3 specifies the operation method: add, sub, mul, div, pow, mod modulo, sqrt to find the arithmetic square root
Addition, subtraction and division: parameter 1 plus / Subtract/multiply/divide by parameter 2
Power: parameter 1 raised to the power of parameter 2.
modulo: the remainder obtained by dividing parameter 1 by parameter 2.
Arithmetic square root: find the arithmetic square root of parameter 1. Parameter 2 does not work , but cannot be omitted.
Readers who are interested in more PHP-related content can check out the special topics of this site: "Summary of PHP operations and operator usage", "Summary of PHP network programming skills", "Introduction to PHP basic syntax", " Summary of php operating office document skills (including word, excel, access, ppt)", "php date and time usage summary", "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php +MySQL database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone in PHP programming.
The above introduces how PHP uses long and large number operations to prevent numbers from being displayed in scientific notation, including scientific notation and number operations. I hope it will be helpful to friends who are interested in PHP tutorials.