©
Ce document utilise Manuel du site Web PHP chinois Libérer
(PHP 4, PHP 5, PHP 7)
is_float — 检测变量是否是浮点型
$var
)
如果 var
是 float 则返回 TRUE
,否则返回 FALSE
。
Note:
若想测试一个变量是否是数字或数字字符串(如表单输入,它们通常为字符串),必须使用 is_numeric() 。
参见 is_bool() 、 is_int() 、 is_integer() 、 is_numeric() 、 is_string() 、 is_array() 和 is_object() 。
[#1] ivan dot maruca at gmail dot com [2015-10-19 23:31:07]
check if string/numeric is float or not :
<?php
function isFloat($value){
return ((int)$value != $value) ;
}
isFloat('1.5') ;//return true
isFloat('15');//return false
isFloat('1e16');//return true
isFloat('foo');//return false
isFloat(5.98);//return true
isFloat(5);//return false
?>
[#2] Erutan409 at Hotmail dot com [2015-05-19 14:37:27]
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:
<?php
if (!function_exists("test_float")) {
function test_float($test) {
if (!is_scalar($test)) {return false;}
$type = gettype($test);
if ($type === "float") {
return true;
} else {
return preg_match("/^\\d+\\.\\d+$/", $test) === 1;
}
}
}
$test = "3.14159265358979323846264338g32795";
var_dump($test);
var_dump((float)$test);
var_dump($test == (string)(float)$test);
var_dump(test_float($test));
?>
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:
<?php
$test = 3.1415926535897932384626433832795;
var_dump($test);
var_dump((float)$test);
var_dump($test == (string)(float)$test);
var_dump(test_float($test));
?>
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.
[#3] nonzer0value [2015-03-26 20:20:44]
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.
<?php
function isfloat($value) {
// PHP automagically tries to coerce $value to a number
return is_float($value + 0);
}
?>
Seems to work ok:
<?php
isfloat("5.0" + 0); // true
isfloat("5.0"); // false
isfloat(5 + 0); // false
isfloat(5.0 + 0); // false
isfloat('a' + 0); // false
?>
YMMV
[#4] KIVagant at gmail dot com [2012-08-21 12:04:30]
Yet another regular expression for float in real life:
<?php
function isTrueFloat($val)
{
$pattern = '/^[-+]?(((\\\\d+)\\\\.?(\\\\d+)?)|\\\\.\\\\d+)([eE]?[+-]?\\\\d+)?$/';
return (!is_bool($val) && (is_float($val) || preg_match($pattern, trim($val))));
}
?>
// 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'
[#5] Mitch [2012-06-12 20:27:13]
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.
[#6] Boylett [2008-09-20 09:29:40]
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);
[#7] ckelley at the ca - cycleworks dot com [2008-07-14 15:39:58]
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.
[#8] kshegunov at gmail dot com [2008-03-31 13:32:24]
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]+))$/
[#9] phper [2006-01-25 12:08:36]
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);