PHP uses a very long (oversized) digital operation to prevent digital display with scientific notation _php skills

Source: Internet
Author: User
Tags arithmetic mul pow square root

The example of this article is about the method of using super long (large) digital operation to prevent digital display with scientific counting method. Share to everyone for your reference, specific as follows:

PHP makes a mistake when calculating large numeric operations, and when the number is too large, the value becomes the scientific count. So how do you do PHP super large numerical operations, including subtraction, power operations, square roots, modulo operations?

To solve the scientific count problem, just add a pair of quotes when assigning a value.

Such as:

<?php
$n = ' 22222222222222222222222222220 ';
echo $n;
? >

If not quoted, show 2.2222222222222E+28, quote 22222222222222222222222222220

Super large numerical operation, including subtraction, power operation, square root, modulo operation.

Use PHP's Bcmath function to create a custom function with the following code,

<?php
Function Calc ($m, $n, $x) {
  $errors =array (
    ' dividend 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 ' sqrt ':
      if ($m >=0) {
        $t =bcsqrt ($m);
      } else{return
        $errors [1];
      }
      break;
  $t =preg_replace ("/\.") *0+$/", ', $t);
  return $t;
}
/* Usage examples
/echo calc (' 11111111111111111111111111111111110 ', ', ', ' Add ');
? >

How to use:

Calc (parameter 1 parameter 2, parameter 3);
Parameter 3 Specifies the method of operation: Add Plus, sub minus, Mul, div except, pow power, mod modulo, sqrt for arithmetic square root
Plus minus good except: Parameter 1 Plus/minus/times/divided by parameter 2
Power: Parameter 1 of the argument is 2 times Square.
Modulus: The remainder obtained by parameter 1 divided by parameter 2.
Arithmetic square root: Calculate the arithmetic square root of parameter 1. Parameter 2 does not work, but cannot be omitted.

More interested in PHP related content readers can view the site topics: "PHP Operations and Operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Basic Grammar Introductory Tutorial", "PHP operation Office Document Skills Summary (including word,excel,access, PPT), "The PHP date and time usage summary", "PHP object-oriented Programming Introduction Course", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

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.