检查数字是否在 PHP 中浮动 [英] checking if a number is float in PHP

查看:30
本文介绍了检查数字是否在 PHP 中浮动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这真的很奇怪.我有这段代码.

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;
}

$amt 的值为 0.01.但它正在进入 else 状态.所以我做了一个 $amt 的 var_dump.它说字符串(4)所以我决定输入 $amt

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;

但问题在于,即使 $amt 的值为 1,它仍然会被类型转换为 float 并进入 if 条件,这不应该发生.有没有其他方法可以做到这一点?谢谢

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

推荐答案

如果把第一行改成

$rewardAmt = $amt+0;

$rewardAmt 应该被转换为一个数字.

$rewardAmt should be cast to a number.

这篇关于检查数字是否在 PHP 中浮动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆