Checking If A String Is A Double Or Not


Answer :

// Try to convert the string to a float $floatVal = floatval($num); // If the parsing succeeded and the value is not equivalent to an int if($floatVal && intval($floatVal) != $floatVal) {     // $num is a float } 

This will omit integer values represented as strings:

if(is_numeric($num) && strpos($num, ".") !== false) {     $this->print_half_star(); } 

You can try this:

function isfloat($num) {     return is_float($num) || is_numeric($num) && ((float) $num != (int) $num); }  var_dump(isfloat(10));     // bool(false) var_dump(isfloat(10.5));   // bool(true) var_dump(isfloat("10"));   // bool(false) var_dump(isfloat("10.5")); // bool(true) 

Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?