Home > Backend Development > PHP Tutorial > PHP uses super long (oversized) number operations to prevent numbers from being displayed in scientific notation, php counting_PHP tutorial

PHP uses super long (oversized) number operations to prevent numbers from being displayed in scientific notation, php counting_PHP tutorial

WBOY
Release: 2016-07-12 08:55:22
Original
1140 people have browsed it

PHP uses super long (very large) number operations to prevent numbers from being displayed in scientific notation, php counting

This article describes an example of PHP using super long (very large) number operations to prevent numbers from being displayed in scientific notation. A method of displaying numbers in scientific notation. Share it with everyone 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 large numerical operations in PHP, including addition, subtraction, multiplication, division, 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:

<&#63;php
$n = '22222222222222222222222222220';
echo $n;
&#63;>

Copy after login

Without quotation marks, it displays 2.2222222222222E 28. With quotation marks, it displays 22222222222222222222222222220

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

Use PHP’s bcmath function to create a custom function, the code is as follows,

<&#63;php
function calc($m,$n,$x){
  $errors=array(
    '被除数不能为零',
    '负数没有平方根'
  );
  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;
}
/*用法举例*/
echo calc('11111111111111111111111111111111110','10','add');
&#63;>

Copy after login

Usage:

calc(parameter 1 parameter 2, parameter 3);
Parameter 3 specifies the operation method: add, sub, mul, div, pow, mod, sqrt to find the arithmetic square root
Addition, subtraction and division: Parameter 1 Add/Subtract/Multiply/Divide Parameter 2
Power: parameter 1 raised to the power of parameter 2.
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.

Readers who are interested in more PHP-related content can check out the special topics on this site: "Summary of PHP operations and operator usage", "Summary of PHP network programming skills", "Introduction to PHP basic syntax", "php operation office documentation" Summary of skills (including word, excel, access, ppt)", "Summary of php date and time usage", "Introduction to php object-oriented programming tutorial", "Summary of php string (string) usage", "Introduction to php mysql database operation" Tutorial" and "Summary of Common PHP Database Operation Skills"

I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

  • PHP numerical operation verification code implementation code
  • PHP implementation of numerical verification code and numerical operation verification code
  • Analysis of PHP mathematical operations and data processing examples
  • A large summary of PHP mathematical operation functions (classics worth collecting)
  • A brief talk about the precise operation of PHP floating point numbers
  • PHP commonly used special operation symbols and function summary (a must-read for newbies to PHP)
  • Chapter 4 PHP Mathematical Operations
  • PHP’s chr and ord functions implement character addition, subtraction, multiplication and division operations implementation code
  • PHP Full Probability operation function (optimized version) Essential for Webgame development
  • php mathematical operation verification code implementation code

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1117046.htmlTechArticlePHP uses overlong (oversized) number operations to prevent numbers from being displayed in scientific notation. PHP counting is an example in this article PHP uses super long (extra large) number operations to prevent numbers from being displayed in scientific notation...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template