类型转换指数数字为int或float [英] type cast exponential number to int or float
本文介绍了类型转换指数数字为int或float的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码:
echo (int) "2e2";
echo (int) 2e2;
echo (float) "2e2";
输出
2
200
200
想法为什么。感谢。
推荐答案
2e2是科学记数法,表示2 * 10 2 == 200。
"2e2" is scientific notation, meaning 2*102 == 200.
- 在第一个示例中,将字符串解析为int只读取第一个非数字的数字(因此忽略
e
)。 - PHP解析器将
2e2
视为 float literal ,值为200.0,当转换为int时,这个值为200。 - 解析字符串作为浮点数可以理解符号, 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
2e2
as 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.
这篇关于类型转换指数数字为int或float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文