Thursday 15 March 2012

Finds whether the type of a variable is true Float or not

The final version of function to check the actual float value and return boolean(true/false).

public function isTrueFloat($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;
}

The following are the test results from the above function.
//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

No comments:

Post a Comment

Please post any queries and comments here.