PHP uses ultra-long (ultra-large) numeric operations to prevent numbers from being displayed in scientific notation.
This example describes how PHP uses ultra-long (ultra-large) numeric operations to prevent numbers from being displayed in scientific notation. We will share this with you for your reference. The details are as follows:
An error occurs when PHP calculates a large number. When the number is too large, the value changes to a scientific count. how can we perform PHP super-large numeric operations, including addition, subtraction, multiplication, division, power operation, square root, modulo operation?
To solve the scientific counting problem, you only need to add a pair of quotation marks when assigning values.
For example:
<?php$n = '22222222222222222222222222220';echo $n;?>
If no quotation marks are added, 2.2222222222222E + 28 is displayed. If no quotation marks are added, 22222222222222222222222222220 is displayed.
Super Large numeric operations, including addition, subtraction, multiplication, division, power operation, square root, modulo operation.
Use the PHP bcmath function to create a UDF. The Code is as follows,
<? Phpfunction calc ($ m, $ n, $ x) {$ errors = array ('divisor cannot be zero ', 'negative number has no square root'); 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 'qrt ': if ($ m> = 0) {$ t = bcsqrt ($ m);} else {return $ errors [1];} break ;} $ t = preg_replace ("/\.. * 0 + $/",'', $ t); return $ t;}/* usage example */echo calc ('20140901', '10 ', 'add');?>
Usage:
Calc (parameter 1 parameter 2 parameter 3 );
Parameter 3 specifies the operation method: add plus, sub subtraction, mul, div division, pow power, mod modulo, and sqrt for arithmetic square root
Addition and subtraction: Add/subtract/multiply/divide by parameter 2
Power: the power of parameter 1 to the power of 2.
Modulo: Divide parameter 1 by the remainder of parameter 2.
Arithmetic square root: calculates the arithmetic square root of parameter 1. Parameter 2 does not work, but cannot be omitted.