Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check that a string is an int, but not a double, etc.?

People also ask

How do I check if a string is an int?

The most efficient way to check if a string is an integer in Python is to use the str. isdigit() method, as it takes the least time to execute. The str. isdigit() method returns True if the string represents an integer, otherwise False .

How do you check if a string is Parsable to int?

Perhaps the easiest and the most reliable way to check whether a String is numeric or not is by parsing it using Java's built-in methods: Integer. parseInt(String) Float.

How do you check if a number is an int?

The Number. isInteger() method returns true if a value is an integer of the datatype Number. Otherwise it returns false .


How about using ctype_digit?

From the manual:

<?php
$strings = array('1820.20', '10002', 'wsl!12');
foreach ($strings as $testcase) {
    if (ctype_digit($testcase)) {
        echo "The string $testcase consists of all digits.\n";
    } else {
        echo "The string $testcase does not consist of all digits.\n";
    }
}
?>

The above example will output:

The string 1820.20 does not consist of all digits.
The string 10002 consists of all digits.
The string wsl!12 does not consist of all digits.

This will only work if your input is always a string:

$numeric_string = '42';
$integer        = 42;

ctype_digit($numeric_string);  // true
ctype_digit($integer);         // false

If your input might be of type int, then combine ctype_digit with is_int.

If you care about negative numbers, then you'll need to check the input for a preceding -, and if so, call ctype_digit on a substr of the input string. Something like this would do it:

function my_is_int($input) {
  if ($input[0] == '-') {
    return ctype_digit(substr($input, 1));
  }
  return ctype_digit($input);
}

filter_var should do it:

var_dump(filter_var('2', FILTER_VALIDATE_INT));   // 2
var_dump(filter_var('2.0', FILTER_VALIDATE_INT)); // false
var_dump(filter_var('2.1', FILTER_VALIDATE_INT)); // false

but

var_dump(filter_var(2, FILTER_VALIDATE_INT));     // 2
var_dump(filter_var(2.0, FILTER_VALIDATE_INT));   // 2
var_dump(filter_var(2.1, FILTER_VALIDATE_INT));   // false

If you just want Booleans as return values, wrap it into a function, e.g.

function validatesAsInt($number)
{
    $number = filter_var($number, FILTER_VALIDATE_INT);
    return ($number !== FALSE);
}

+1 to Dominic's answer (using ctype_digit). Another way you could do it is with type coercion:

$inty = "2";
$inty2 = " 2";
$floaty = "2.1";
$floaty2 = "2.0";

is_int($inty + 0); // true
is_int($floaty + 0); // false
is_int($floaty2 + 0); // false

// here's difference between this and the ctype functions.
is_int($inty2 + 0);  // true
ctype_digit($inty2); // false

Cast it to int. if it still have the same value its int;

function my_is_int($var) {
  $tmp = (int) $var;
  if($tmp == $var)
       return true;
  else
       return false;
}

Had a need for a robust is_int recently. I found intval() too unpredictable:

intval(array('foo', 'bar')) //returns 1 ?!?
intval("2dog") //returns 2 even though the value is definitely not an integer
intval("dog2") //also returns 2


Came across this snippet in the PHP documentation comments, and after testing it, it covers almost everything you throw at it:

function my_is_int($s) {
    return (is_numeric($s) ? intval($s) == $s : false);
}


my_is_int(2); //true
my_is_int("2"); //true
my_is_int(2.1); //false
my_is_int("2.1"); //false
my_is_int("dog"); //false
my_is_int("2dog"); //false
my_is_int("dog2"); //false
my_is_int(array('foo', 'bar')); //false
my_is_int(array(1)); //false


But careful:

my_is_int(2.0); //true
my_is_int("2.0"); //true

One really clean way that I like to use is that one. You cast it twice, first in int, secondly in string, then you strict compare ===. See the example below:

$value === (string)(int)$value;

Now, about your function my_is_int, you can do something like this:

function my_is_int($value){ return $value === (string)(int)$value; }
my_is_int('2');  // true
my_is_int('2.1') // false