9

Why does echo 100000000000000; output 1.0E+14 and not 100000000000000?

This kind of transformation of integers on output happens only for integers that are 15 digits long and longer.

Emanuil Rusev
  • 34,563
  • 55
  • 137
  • 201
  • 2
    To be fair, the other question's title isn't worded very nicely. – Jimbo Mar 15 '13 at 16:30
  • 1
    `echo number_format(100000000000000,0,null,'');` – Antony Mar 15 '13 at 16:32
  • 1
    @PHPNooB OP asks way more questions than provides answers, so the *rep* is from asking questions and as such I believe he would not know this as his profile does not indicate he is a PHP expert. Also, as Jimbo pointed out the title is indeed badly worded. Please do some research like clicking the profile, before you accuse the OP of badge whoring. – kittycat Mar 15 '13 at 16:47
  • @Jimbo Doesn't really matter. The point of flagging duplicates is to redirect to answers and avoid duplication of efforts, not to nag at the OP for not searching enough. (That said, it's not exactly a duplicate because that question is about numbers that certainly can be represented as integers.) – millimoose Mar 15 '13 at 16:50
  • @LifeInTheGrey, The question you link to, asks about floats, while my question asks about integers. – Emanuil Rusev Mar 15 '13 at 17:09
  • @LifeInTheGrey Although the core answer might be the same, the questions are approaching the problem from two different sides. And as you can see the answers in the other question are in a quite different direction. – Haralan Dobrev Mar 15 '13 at 17:09
  • 1
    yeah my bad, its very similar but not identical; agree with @HaralanDobrev that it is the same question and answer but coming from the opposite direction. and my apologies for accusing the OP of badge whoring ... that was inappropriate, and i never should have said that. – PlantTheIdea Mar 15 '13 at 17:23

5 Answers5

11

PHP will convert an integer to float if it is bigger 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 the scientific notation is bigger than 13 or smaller than -4, PHP will use 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

Community
  • 1
  • 1
Haralan Dobrev
  • 7,617
  • 2
  • 48
  • 66
6

That number is too big to fit into a 32 bit integer so PHP is storing it in a float.

See the integer overflow section in the php manual.

On the off chance that you need really big integers, look into GMP.

Jacob Parker
  • 2,546
  • 18
  • 31
3

PHP cannot handle integers that big, and therefore treats them as floats. Floats are typically represented in scientific notation to take into account the inaccuracies past a certain number of significant digits.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
2

The number is too big to be stored as integer by PHP on your platform, so it is stored as a floating-point number. The rules of conversion to string are different for float-numbers and integers.

Try the following:

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

Output:

float(1.0E+14) 
not an integer
Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
0

Any number larger than PHP's built-in integer size is stored and represented as a float.

To format a number in a particular way on output, use a function such as printf .

Ryan
  • 26,884
  • 9
  • 56
  • 83