PHP 字符串,小数点到数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10979909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP string with decimals to number
提问by Ilya Karnaukhov
So when I try the following:
因此,当我尝试以下操作时:
$a = '1.00';
$b = '1.01';
if($a < $b){
print 'ok';
}
This works fine. But when I retrieve these variables from an xml file. the strings are EXACTLY the same, but for some reason, the if function doesn't work right. So I assume I have to convert the strings to a number. But when I do so, the decimals get removed.
这工作正常。但是当我从 xml 文件中检索这些变量时。字符串完全相同,但由于某种原因,if 函数无法正常工作。所以我假设我必须将字符串转换为数字。但是当我这样做时,小数会被删除。
Is my assumption correct? If so, how do i solve it?
我的假设正确吗?如果是这样,我该如何解决?
If not, what's the problem?
如果不是,有什么问题?
Thank you!
谢谢!
回答by Geert
$a = (float) $a;
$b = (float) $b;
Related reading: http://php.net/manual/en/language.types.type-juggling.php
相关阅读:http: //php.net/manual/en/language.types.type-juggling.php
回答by Ilya Karnaukhov
Found the answer by googling :)
通过谷歌搜索找到答案:)
floatval($string);

