Variable handling 函数
在线手册:中文 英文
PHP手册

is_float

(PHP 4, PHP 5)

is_float检测变量是否是浮点型

描述

bool is_float ( mixed $var )

如果 varfloat 则返回 TRUE,否则返回 FALSE

Note:

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

参见 is_bool()is_int()is_integer()is_numeric()is_string()is_array()is_object()


Variable handling 函数
在线手册:中文 英文
PHP手册
PHP手册 - N: 检测变量是否是浮点型

用户评论:

wschalle {A}{T} gmail {D}{O}{T} com (15-Mar-2012 09:25)

Alas, the final version listed below by ircadhikari still has some flaws. Here is a fixed version with test output. Not sure how it would do with unicode, but I'm beyond caring at this point. YMMV.

<?php
//New function -- works
function isFloat($val){   
  if(
is_string($val)) $val = trim($val);
  if(
is_numeric($val) && ( is_float($val) || ( (float) $val > (int) $val
  
|| strlen($val) != strlen( (int) $val) ) && (ceil($val)) != 0 )) return true;
  else return
false;
}

//Old function -- breaks in some cases
function isTrueFloat($val){ 
  if(
is_float($val) || ( (float) $val > (int) $val
   
|| strlen($val) != strlen( (int) $val) ) && (ceil($val)) != 0 ) return true;
  else return
false;
}
?>
<?php
//tests
function tf($val) {
  echo
$val . "\t";
  echo (
isFloat($val))?'true':'false';
  echo
"\t";
  echo (
isTrueFloat($val))?'true':'false';
  echo
"\t";
  echo (
is_numeric($val))?'true':'false';
  echo
"\t";
  echo (
is_string($val))?'true':'false';
  echo
"\n";
}
echo
"value\ttfloat\toldfunc\tnumeric\tstring\n";

//And here's the tests with the test output appended
              // value   tfloat  oldfunc numeric string
tf("2.4");    // 2.4     true    true    true    true
tf("2");      // 2       false   false   true    true
tf('4.0');    // 4.0     true    true    true    true
tf('2.1');    // 2.1     true    true    true    true
tf(0);        // 0       false   false   true    false
tf("0");      // 0       false   false   true    true
tf(3.);       // 3       true    true    true    false
tf(13);       // 13      false   false   true    false
tf("12");     // 12      false   false   true    true
tf(3.53);     // 3.53    true    true    true    false
tf("0.27");   // 0.27    true    true    true    true
tf(0.24);     // 0.24    true    true    true    false
tf('-24.1');  // -24.1   true    true    true    true
tf(-24.121);  // -24.121 true    true    true    false
//While the tests below are not strictly numbers, they should not evaluate as floats.
tf("1num");   // 1num    false   true    false   true
tf("num1");   // num1    false   false   false   true
tf(" 1");     //  1      false   true    true    true
tf("1 ");     // 1       false   true    false   true
tf("num1.4"); // num1.4  false   false   false   true
tf("num 1.4");// num 1.4 false   false   false   true
tf("1.4num"); // 1.4num  false   true    false   true
tf("num1.4"); // num1.4  false   false   false   true

ircadhikari at gmail dot com (15-Mar-2012 02:22)

The final version of function to check the actual float value based on previous Kenaniah's version.
<?php
public function isTrueFloat($val){   
       
        if(
is_float($val) || ( (float) $val > (int) $val || strlen($val) != strlen( (int) $val) ) && (ceil($val)) != 0 ) return true;
        else return
false;
    }
?>

<?php
//Tests
'4.0'       returns true
'2.1'       returns true
0           returns false
"0"         returns false
3.          returns true
13          returns false
"12"        returns false
3.53        returns true
// Additional Tested ones
"0.27"      returns true
0.24        returns true
?>

Boylett (20-Sep-2008 05:29)

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

ckelley at the ca - cycleworks dot com (14-Jul-2008 11:39)

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.

kshegunov at gmail dot com (31-Mar-2008 09:32)

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]+))$/

celelibi at gmail dot com (03-Mar-2008 04:31)

Unlike others comment may let you think, this function tests *only* the type of the variable. It does not perform any other test.
If you want to check if a string represents a valid float value, please use is_numeric instead.

WHITE new media architects - Jeroen (10-Jan-2008 11:24)

The above printed "is_true_float" function is not correct.
It gives wrong answers on the following tests:
is_true_float("-4,123"); # returns false
is_true_float("0,123");  # returns false

So I changed the function to correctly handle qouted negative floats and quoted floats near zero:

<?php

function is_true_float($mVal)
{
  return (
is_float($mVal)
           || ( (float)
$mVal != round($mVal)
                ||
strlen($mVal) != strlen( (int) $mVal) )
              &&
$mVal != 0 );
}

?>

Kenaniah Cerny (06-Nov-2007 07:21)

For those of you who have discovered that is_float() does not behave exactly the way you would expect it to when passing a string, here is a function that extends is_float properly report floating numbers given any sort of mixed variable.

<?php
function is_true_float($val){
    if(
is_float($val) || ( (float) $val > (int) $val || strlen($val) != strlen( (int) $val) ) && (int) $val != ) return true;
    else return
false;
}
?>

<?php
//Tests
'4.0'       returns true
'2.1'       returns true
0           returns false
"0"         returns false
3.          returns true
13          returns false
"12"        returns false
3.53        returns true
?>

Enjoy

phper (25-Jan-2006 08:08)

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

kirti dot contact at gmail dot com (19-Oct-2005 07:18)

To check a float only should contain certain number of decimal places, I have used this simple function below

<?
function is_deccount($number,$decimal=2){
    $m_factor=pow(10,$decimal);
    if((int)($number*$m_factor)==$number*$m_factor)
        return true;
    else
        return false;
 }   
?>