PHP uses ultra-long (ultra-large) numeric operations to prevent digital display by scientific notation. php counting _ PHP Tutorial

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.