type cast exponential number to int or float

左心房为你撑大大i 提交于 2019-12-20 02:10:34

问题


The following code:

echo (int) "2e2";
echo (int) 2e2;
echo (float) "2e2";

outputs

2
200
200

.. and i have no idea why. Thanks.


回答1:


"2e2" is scientific notation, meaning 2*102 == 200.

  1. In your first example, parsing the string as an int reads only digits up to the first non-digit (so it ignores the e).
  2. The PHP parser treats 2e2 as a float literal with value 200.0 and this gives 200 when cast to a int.
  3. Parsing the string as a float understands the notation and gives the expected result of 200.


来源:https://stackoverflow.com/questions/8090275/type-cast-exponential-number-to-int-or-float

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!