(PHP 4, PHP 5)
is_float — Prüft, ob eine Variable vom Typ float ist
Prüft, ob eine Variable vom Typ float ist
Hinweis:
Um zu testen, ob eine Variable eine Zahl oder eine numerische Zeichenkette ist (wie zum Beispiel Formularangaben, die immer Zeichenketten sind), müssen Sie is_numeric() verwenden.
Die auszuwertende Variable.
Gibt TRUE zurück, wenn var vom Typ float ist, sonst FALSE.
Beispiel #1 is_float()-Beispiel
<?php
if(is_float(27.25)) {
echo "is float\n";
}else {
echo "is not float\n";
}
var_dump(is_float('abc'));
var_dump(is_float(23));
var_dump(is_float(23.5));
var_dump(is_float(1e7)); //Scientific Notation
var_dump(is_float(true));
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
is float bool(false) bool(false) bool(true) bool(true) bool(false)
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);
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.
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]+))$/
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.
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 );
}
?>
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 != 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
?>
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);
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;
}
?>