百科狗-知识改变命运!
--

is_float() - php 变量处理函数

乐乐1年前 (2023-11-21)阅读数 19#技术干货
文章标签字符串

is_float()

(PHP 4, PHP 5, PHP 7)

检测变量是否是浮点型

描述

is_float(mixed $var): bool

is_float() - php 变量处理函数

如果$var是float则返回TRUE,否则返回FALSE

Note:

若想测试一个变量是否是数字或数字字符串(如表单输入,它们通常为字符串),必须使用is_numeric()。

参见is_bool()、is_int()、is_integer()、is_numeric()、is_string()、is_array()和is_object()。

Boylett's solution is elegant (http://php.net/manual/en/function.is-float.php#85848), but won't work for long float's or variables that are not explicitly type of 'string' or for long floats that are encased in quotes, making it a string that will be truncated/rounded when cast to a float. So, further logic must be completed to test for the case. Take the following example:

Will produce (32-bit):
string(34) "3.14159265358979323846264338g32795"
float(3.1415926535898)
bool(false)
bool(false)
So far, so good, right? Yeah, but it's misleading, because the string is so long, that when it's converted to a float, it won't be equivalent to the comparison of the value being cast back into a string . So the aforementioned short function works. Look at this next example:

Will produce (32-bit):
float(3.1415926535898)
float(3.1415926535898)
bool(false)
bool(true)
Why is it not working now, but the value is truly a float? Same reasoning as mentioned before. The float is so long that it's truncated/rounded and doesn't match the comparison being done with the short-hand function.
So, as you can see, more logic should be applied to the variable you're testing.
Coercing the value to float and back to string was a neat trick. You can also just add a literal 0 to whatever you're checking.

Seems to work ok:

YMMV
If you want to test whether a string is containing a float, rather than if a variable is a float, you can use this simple little function:
function isfloat($f) return ($f == (string)(float)$f);
As celelibi at gmail dot com stated, is_float checks ONLY the type of the variable not the data it holds!
If you want to check if string represent a floating point value use the following regular expression and not is_float(),
or poorly written custom functions.
/^[+-]?(([0-9]+)|([0-9]*\.[0-9]+|[0-9]+\.[0-9]*)|
(([0-9]+|([0-9]*\.[0-9]+|[0-9]+\.[0-9]*))[eE][+-]?[0-9]+))$/
Yet another regular expression for float in real life:

// Matches:
1, -1, 1.0, -1.0, '1', '-1', '1.0', '-1.0', '2.1', '0', 0, ' 0 ', ' 0.1 ', ' -0.0 ', -0.0, 3., '-3.', '.27', .27, '-0', '+4', '1e2', '+1353.0316547', '13213.032468e-13465', '-8E+3', '-1354.98879e+37436'
// Non-matches:
false, true, '', '-', '.a', '-1.a', '.a', '.', '-.', '1+', '1.3+', 'a1', 'e.e', '-e-4', 'e2', '8e', '3,25'
Another RegEx for matching floats:
/^[+-]?(\d*\.\d+([eE]?[+-]?\d+)?|\d+[eE][+-]?\d+)$/
Matches:
1e2
+1353.0316547
13213.032468e-13465
-8E+3
-1354.98879e+37436
Non-matches:
8
e2
-e-4
E
asdf.safd
8e
Enjoy.
A better way to check for a certain number of decimal places is to use :
$num_dec_places = 2;
number_format($value,$num_dec_places);
check if string/numeric is float or not : 
Personally, I use an implicit cast:
if( is_float($value+1) ){
  $value=sprintf("%.2f",$value);
}
Which turns 22.0000000 query result into 22.00 for display to users.

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)