PHP uses extra-long (extra-Large) numeric operations to prevent numbers from being displayed in scientific notation, and PHP counts
In this paper, the method of using extra-long (extra-large) digital arithmetic to prevent the digital display by scientific counting method is described. Share to everyone for your reference, as follows:
PHP will make an error when calculating a large numeric operation, and when the number is too large, the value will be counted as scientific. So how do you do PHP super-large numeric operations, including subtraction, exponentiation, square root, modulo operations?
To solve the problem of scientific counting, just add a pair of quotes when assigning a value.
Such as:
<?php$n = ' 22222222222222222222222222220 '; echo $n;? >
If not quoted, display 2.2222222222222E+28, quote 22222222222222222222222222220
Extra-large numerical operations, including subtraction, power operations, square roots, modulo operations.
Use PHP's Bcmath function to create a custom function with the following code,
<?phpfunction Calc ($m, $n, $x) { $errors =array ( ' dividend cannot be zero ', ' negative numbers have 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 ' sqrt ': if ($m >=0) { $t =bcsqrt ($m); } else{ return $errors [1]; } break; } $t =preg_replace ("/\. *0+$/",", $t); return $t;} /* Usage Example */echo calc (' 11111111111111111111111111111111110 ', ' ten ', ' Add ');? >
How to use:
Calc (parameter 1, parameter 2, parameter 3);
Parameter 3 Specifies the method of operation: Add, Sub minus, Mul, div except, pow power, mod modulo, sqrt to find the square root of arithmetic
Add and subtract: Parameter 1 plus/minus/multiply/divide by parameter 2
Power: Parameter 1 of the parameter 2 times Square.
Modulo: The remainder of the parameter 1 divided by the parameter 2.
Arithmetic square root: The arithmetic square root of parameter 1. Parameter 2 does not work, but cannot be omitted.
More readers interested in PHP related content can view this site: "Summary of PHP operations and operator usage," PHP Network Programming Tips Summary, "PHP Basic Grammar Primer Tutorial", "PHP operation Office Document tips summary (including word,excel,access, ppt), "PHP Date and Time usage summary", "PHP primer for Object-oriented programming", "PHP String Usage Summary", "Getting Started with Php+mysql database operations" and "PHP Common Database Operations Skills Summary"
I hope this article is helpful to you in PHP programming.
Articles you may be interested in:
- Implementation code of PHP digital Operation verification Code
- PHP implementation of digital verification code and digital operation verification Code
- Analysis of PHP mathematical operation and Data processing example
- PHP Math function Summary (classic worth collecting)
- Simple talk about the exact operation of PHP floating point numbers
- PHP Common special operation symbols and functions summary (PHP Novice must SEE)
- The fourth chapter of the PHP math operation
- PHP's CHR and ORD functions implement the character subtraction operation implementation code
- PHP Full probability arithmetic function (optimized version) Webgame Development essentials
- Implementation Code of PHP math Verification Code
http://www.bkjia.com/PHPjc/1117046.html www.bkjia.com true http://www.bkjia.com/PHPjc/1117046.html techarticle PHP using extra-long (extra-large) digital operations to prevent the number of scientific notation to display the method, PHP count This article describes the PHP use of extra-long (super-Large) digital operation to prevent the number of scientific counting method ...