Dmitry Makovetskiyd
Dmitry Makovetskiyd

Reputation: 7053

Checking if a string is a double or not

I am trying to check in php if a string is a double or not.

Here is my code:

   if(floatval($num)){
         $this->print_half_star();
    }

$num is a string..The problem is that even when there is an int it gives true. Is there a way to check if it is a float and not an int!?

Upvotes: 18

Views: 21112

Answers (8)

Theodore R. Smith
Theodore R. Smith

Reputation: 23231

Here's what I ended up creating, for modern PHP:

/**
 * Determines if a variable appears to be a float or not.
 *
 * @param string|int|double $number
 * @return bool True if it appears to be an integer value. "75.0000" returns false.
 * @throws InvalidArgumentException if $number is not a valid number.
 */
function isFloatLike($number): bool
{
    // Bail if it isn't even a number.
    if (!is_numeric($number)) {
        throw new InvalidArgumentException("'$number' is not a valid number.");
    }

    // Try to convert the variable to a float.
    $floatVal = floatval($number);

    // If the parsing succeeded and the value is not equivalent to an int, 
    return ($floatVal && intval($floatVal) != $floatVal);
}

Upvotes: 0

EyeOfTheHawks
EyeOfTheHawks

Reputation: 576

You could just check if the value is numeric, and then check for a decimal point, so...

if(is_numeric($val) && stripos($val,'.') !== false)
{
    //definitely a float
}

It doesn't handle scientific notation very well though, so you may have to handle that manually by looking for e

Upvotes: 1

user6232569
user6232569

Reputation: 1

In order to get all cases of a float we've got to add any zero's back on to the end that may be chopped off by floatval() or by typecasting (float)$num

$num='17.000010';
$num_float = (float)$num; //$num_float is now 17.00001  
//add the zero's back to $num_float
while (strlen ($num) > strlen ($num_float))  $num_float = $num_float . '0';   //$spot_float in now a string again


if($num_float != $num) then $num was no double :;

note !== was not used just in case $num_float was never converted back to a string by adding zeroes. such would be the case for values that didn't end in 0.

Upvotes: 0

voodoo417
voodoo417

Reputation: 12101

if "double string format " like this "XXXXXX.XXXXXX"

try check

function check_double_string($str){
 $pairs = explode('.',$str);
 if ( is_array($pairs) && count($pairs)==2) {
   return ( is_numeric($pairs[0]) && is_numeric($pairs[1])? true : false; 
 }
 return false;
}

Upvotes: 0

ARIF MAHMUD RANA
ARIF MAHMUD RANA

Reputation: 5166

Why not use the magic of regular expression

<?php
$p = '/^[0-9]*\.[0-9]+$/';
$v = '89.00';
var_dump(preg_match($p, $v));
$v = '.01';
var_dump(preg_match($p, $v));
$v = '0.01';
var_dump(preg_match($p, $v));
$v = '89';
var_dump(preg_match($p, $v));

Upvotes: 0

Florent
Florent

Reputation: 12420

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)

Upvotes: 8

Samuel
Samuel

Reputation: 17171

// 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
}

Upvotes: 25

JK.
JK.

Reputation: 5136

This will omit integer values represented as strings:

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

Upvotes: 12

Related Questions