PHP uses ultra-long (ultra-large) numeric operations to prevent numbers from being displayed in scientific notation. php counts. PHP uses ultra-long (ultra-large) numeric operations to prevent numbers from being displayed in scientific notation. This article describes how php uses ultra-long (ultra-large) digital operations prevent numbers from being displayed by scientific counting methods PHP uses ultra-long (ultra-large) numeric operations prevent numbers from being displayed by scientific notation, php counts
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 'P': if ($ n! = 0) {$ t = bcp ($ 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, p 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.