Type Cast Exponential Number to Int or Float
The Following Code: Echo (Int) "2E2"; Echo (Int) 2E2; Echo (Float) "2E2"; Outputs 2 200 200. . and I Have No Idea Why. Thanks. 1 Answer "2E2" Is Scientific...
The following code:
echo (int) "2e2";
echo (int) 2e2;
echo (float) "2e2";
outputs
2
200
200
.. and i have no idea why. Thanks.
1 Answer
"2e2" is scientific notation, meaning 2*102 == 200.
- In your first example, parsing the string as an int reads only digits up to the first non-digit (so it ignores the
e). - The PHP parser treats
2e2as a float literal with value 200.0 and this gives 200 when cast to a int. - Parsing the string as a float understands the notation and gives the expected result of 200.