Home  >  Article  >  Backend Development  >  Examples of PHP using long-digit number operations to prevent numbers from being displayed in scientific notation

Examples of PHP using long-digit number operations to prevent numbers from being displayed in scientific notation

*文
*文Original
2017-12-29 19:03:262192browse

This article mainly introduces the method of PHP using super long (super large) number operations to prevent numbers from being displayed in scientific notation, and involves related skills in PHP mathematical operations and string operations. I hope to be helpful. The example in this article describes how PHP uses extremely long (extra large) number operations to prevent numbers from being displayed in scientific notation. I would like to share them with you for your reference.

The details are as follows:

PHP will make errors when calculating large numerical operations. When the number is too large, the value will become scientific notation. So how to perform PHP extremely large numerical operations, including addition, subtraction, multiplication and division? , what about power operations, square roots, and modulo operations?

To solve the problem of scientific notation, just add a pair of quotation marks when assigning values.

For example:

<?php
$n = &#39;22222222222222222222222222220&#39;;
echo $n;
?>

If Without quotation marks, it displays 2.2222222222222E+28. With quotation marks, it displays 222222222222222222222222222220

Extremely large numerical operations, including addition, subtraction, multiplication and division, power operations, square roots, and modulo operations.

Use PHP's bcmath function Create a custom function, the code is as follows,

<?php
function calc($m,$n,$x){
  $errors=array(
    &#39;被除数不能为零&#39;,
    &#39;负数没有平方根&#39;
  );
  switch($x){
    case &#39;add&#39;:
      $t=bcadd($m,$n);
      break;
    case &#39;sub&#39;:
      $t=bcsub($m,$n);
      break;
    case &#39;mul&#39;:
      $t=bcmul($m,$n);
      break;
    case &#39;p&#39;:
      if($n!=0){
        $t=bcp($m,$n);
      }else{
        return $errors[0];
      }
      break;
    case &#39;pow&#39;:
      $t=bcpow($m,$n);
      break;
    case &#39;mod&#39;:
      if($n!=0){
        $t=bcmod($m,$n);
      }else{
        return $errors[0];
      }
      break;
    case &#39;sqrt&#39;:
      if($m>=0){
        $t=bcsqrt($m);
      }else{
        return $errors[1];
      }
      break;
  }
  $t=preg_replace("/\..*0+$/",&#39;&#39;,$t);
  return $t;
}
/*用法举例*/
echo calc(&#39;11111111111111111111111111111111110&#39;,&#39;10&#39;,&#39;add&#39;);
?>

Usage method:

calc(parameter 1 parameter 2, parameter 3);
Parameter 3 specifies the operation method: add plus, sub minus ,mul good, p division, pow power, mod modulo, sqrt to find the arithmetic square root
Addition, subtraction and division: parameter 1 plus/subtract/multiply/divide parameter 2
Power: parameter of parameter 1 2nd power.
Modulo: The remainder obtained by dividing parameter 1 by parameter 2.
Arithmetic square root: Find the arithmetic square root of parameter 1. Parameter 2 has no effect, but cannot be omitted.

Related recommendations:

php Detailed introduction to operators and expressions_php examples

php basic Function

PHP 6:PHP basic data type

The above is the detailed content of Examples of PHP using long-digit number operations to prevent numbers from being displayed in scientific notation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn