本文介绍了检查一个数字是否在PHP中浮动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这真的很奇怪 $ rewardAmt = $ amt;
if(is_float($ rewardAmt)){
print_r(is float); die;
} else {
print_r(is not float);死;
}
$ amt的值为0.01。但它正在进入其他条件。所以我做了一个$ amt var_dump。它说的字符串(4)
所以我决定改型$ amt
$ rewardAmt =(float)$ amt;
但是这个问题是即使$ amt的值是1,它仍然会被转换为浮动并进入条件,这不应该发生。有没有其他方法可以做到这一点?谢谢 > $ rewardAmt = $ amt + 0;
$ rewardAmt应该转换为数字。
This is really weird. I have this piece of code.
$rewardAmt = $amt;
if(is_float($rewardAmt)){
print_r("is float");die;
} else {
print_r("is not float"); die;
}
value of $amt is 0.01. But it is going into else condition. So I did a var_dump of $amt. it says string(4)So I decided to typecast $amt
$rewardAmt = (float)$amt;
But the problem with this is even if the value of $amt is 1, it still gets typecast to float and goes into if condition, which shouldn't happen. Is there any other way to do this ? Thanks
解决方案
If you change the first line to
$rewardAmt = $amt+0;
$rewardAmt should be cast to a number.
这篇关于检查一个数字是否在PHP中浮动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!