Why PHP Converts 15 + Integer Digits to Output

Why echo 100000000000000; outputs 1.0E+14 , and not 100000000000000 ?

Such conversion of integers at the output occurs only for integers that are 15 digits long and longer.

+6
source share
5 answers

PHP converts an integer to a float if it is greater than PHP_INT_MAX. For 32-bit systems, this is 2147483647.

The answer to the questions is related to the string representation of floats in PHP.

If the exponent in scientific notation is greater than 13 or less than -4 , PHP will use the scientific representation when printing floats.

Examples:

 echo 0.0001; // 0.0001; echo 0.00001; // 1.0E-5 // 14 digits echo 10000000000000; // 10000000000000 // 15 digits echo 100000000000000; // 1.0E+14 

See float vs. double precision

+10
source

This number is too large to fit into a 32-bit integer, so PHP stores it in a float.

See the integer overflow section in php manual .

In case of unforeseen circumstances, you will need really large integers, see GMP .

+6
source

PHP cannot process integers that are large, and therefore treats them as a float. Floats are usually presented in scientific notation to take into account inaccuracies that have passed a certain number of significant numbers.

+3
source

The number is too large to be stored as a PHP integer on your platform, so it is saved as a floating point number. The rules for converting to a string are different for floating point and integer numbers.

Try the following:

 var_dump(100000000000000); echo(is_int(100000000000000) ? 'an integer' : 'not an integer'); 

Output:

 float(1.0E+14) not an integer 
+2
source

Any number greater than the PHP native integer size is stored and represented as a float.

To format the number in a specific way in the output, use a function such as printf .

0
source

All Articles