Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Variable Handling Functions : is_float

is_float

Finds whether the type of a variable is float (PHP 4, PHP 5)
bool is_float ( mixed var )

Example 2591. is_float() example

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

The above example will output:

is float
bool(false)
bool(false)
bool(true)
bool(true)
bool(false)

Code Examples / Notes » is_float

kirti dot contact

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


kenaniah cerny

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


phper

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


Change Language


Follow Navioo On Twitter
debug_zval_dump
doubleval
empty
floatval
get_defined_vars
get_resource_type
gettype
import_request_variables
intval
is_array
is_binary
is_bool
is_buffer
is_callable
is_double
is_float
is_int
is_integer
is_long
is_null
is_numeric
is_object
is_real
is_resource
is_scalar
is_string
is_unicode
isset
print_r
serialize
settype
strval
unserialize
unset
var_dump
var_export
eXTReMe Tracker