PHP uses extra-long, large numbers to prevent numbers from being displayed in scientific notation scientific counting method practice scientific counting method conversion scientific counting method

Source: Internet
Author: User
Tags getting started with php mul
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.

The above is introduced in PHP using extra-long large number of digital operations to prevent the number of scientific counting method, including the scientific counting method, the content of the digital operation, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.