Use PHP functions in JavaScript

JavaScript is_int

!No description available for is_int. @php.js developers: Please update the function summary text file.

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
function is_int (mixed_var) {
    // !No description available for is_int. @php.js developers: Please update the function summary text file.
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/is_int    // +   original by: Alex
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    revised by: Matt Bradley
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: WebDevHobo (http://webdevhobo.blogspot.com/)    // +   improved by: Rafał Kukawski (http://blog.kukawski.pl)
    // %        note 1: 1.0 is simplified to 1 before it can be accessed by the function, this makes
    // %        note 1: it different from the PHP implementation. We can't fix this unfortunately.
    // *     example 1: is_int(23)
    // *     returns 1: true    // *     example 2: is_int('23')
    // *     returns 2: false
    // *     example 3: is_int(23.5)
    // *     returns 3: false
    // *     example 4: is_int(true)    // *     returns 4: false
    return mixed_var === ~~mixed_var;
}
external links: original PHP docs | raw js source

Examples

» Example 1

Running

1
is_int(23)

Should return

1
true

» Example 2

Running

1
is_int('23')

Should return

1
false

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have is_int goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
CoursesWeb
Apr 30th Permalink

q  Hi,
For is_int i use this version:

function is_int(n) {
  return typeof(n)==="number" && Math.round(n) == n;
}

Gravatar
Rafał Kukawski
23 Mar '11 Permalink

q  even shorter

return mixed_var === (mixed_var | 0);

Gravatar
Enrique Melendez
23 Mar '11 Permalink

q  version in one line:

return typeof mixed_var !== 'number' ? false : !(mixed_var % 1);

Gravatar
Kevin van Zonneveld
14 Dec '09 Permalink

q  @ WebDevHobo: Thanks I fixed the link to the php docs throughout the site. As for is_int: yeah that's a real bummer. We can get close to PHP but sometimes it just isn't possible to nail it.
Did fix the jslint warning though, will disappear soon ; )

Gravatar
WebDevHobo
13 Dec '09 Permalink

q  Nevermind my comment on the 0.0 issue, I should read the documentation before saying stuff.

Gravatar
WebDevHobo
13 Dec '09 Permalink

q  Also, the link to the PHP is_int function(to the official PHP docs) is broken. Apparently, not an underscore, but a minus sign is used in the link.

My guess: they want to reserve underscores for replacing spaces.

Gravatar
WebDevHobo
13 Dec '09 Permalink

q  

is_int(0.0)

returns true

Screenshot: http://i.imgur.com/rvtAc.png
Any number of 0's may be added after the . sign, result stays true. The moment you add any other number somewhere behind the . sign, result becomes false.

Supposed bugfix: Check for the . sign and return false whenever encountered. Not sure how to do that, which is why I didn't code it myself.

That is, if we [b]want[/b] this to return false? 0.0 is the exact same as 0, far as I know.

Also: "On line: #25: Confusing use of '!'."

Far as I know, what happens here is the "order of execution". The parser will first execute that within the () and then the ! will be applied to the result, which is what is needed. Not very confusing.

Gravatar
Brett Zamir
7 Nov '09 Permalink

q  @WebDevHobo: Thanks for pointing out those useful functions. We needed the extra checks in our is_numeric(), though I realized later (see http://github.com/kvz/phpjs/commit/eb83b48c940e4f8c5548d63c178c2fae2c0ba729 ) that we also needed to prevent arrays being treated as numeric. As far as integer checking, while we still needed our check for type (e.g., your isInteger() will return true for true), the use of modulus is I believe a sound replacement for the more complex comparison we had earlier. Fixed at http://github.com/kvz/phpjs/commit/06388a893c7a5bcb1876ada68997c04241fc6d52 . Thanks for the functions!

Gravatar
WebDevHobo
30 Oct '09 Permalink

q  Interesting implementation. I'm not that big with Javascript, so this is real nice.

Here's 2 functions that I usually use for this kind of stuff:

function isInteger(value){
  return (!(value % 1));
}

function isNumber(n){
  return n != null && n != "" && typeof(n) != "boolean" && !isNaN(n);
}



Don't know what flaws might be in them, but they sure did the trick uptill now.

Gravatar
Brett Zamir
8 Sep '09 Permalink

q  @Jordan: It's false for me (as it should be)--not sure what you mean...

Gravatar
Jordan
8 Sep '09 Permalink

q  

is_int('23,5') return true...


Gravatar
Kevin van Zonneveld
25 Jan '09 Permalink

q  @ Matt Bradley: Thanks for you function. The current implementation did as well. But in fact, I checked the PHP manual: and that's not supposed to happen. So I've actually changed your implementation that also the type is looked at. And now all the examples in the php manual workout with our function as well.

Gravatar
Matt Bradley
20 Jan '09 Permalink

q  This version will also work with strings

function is_int(value){
if(parseFloat(value) == parseInt(value)){
return true;
} else {
return false;
}
}

Gravatar
Kevin van Zonneveld
3 Dec '08 Permalink

q  @ Paulo Ricardo F. Santos: OK, I've added a note to the is_int code, we'll leave it at that then.

Gravatar
Paulo Ricardo F. Santos
3 Dec '08 Permalink

q  @ Kevin: Yep, the value is evaluated before it is assigned - this should occurs through the Number object constructor. A time that JS lacks different objects to Float/Integer values, I think that, unfortunately, there's no way to match this approach. ;/

Gravatar
Kevin van Zonneveld
1 Dec '08 Permalink

q  @ Paulo Ricardo F. Santos: thanks. I believe that 1.0 is simplified to 1 as soon before it can be accessed by the function. if I echo mixed_var in the function's first statement, it's already 1. So if anybody knows a good way to get this example working:

[CODE="Javascript"]
// * example 3: is_int(1.0);
// * returns 3: false
[/CODE]

please let me know!

Gravatar
Paulo Ricardo F. Santos
29 Nov '08 Permalink

q  Note: both current/my implementation isn't PHP compliant at all, since in JS '1.0 === 1' is a True comparison.

Gravatar
Paulo Ricardo F. Santos
28 Nov '08 Permalink

q  KISS way: just do value/type comparison with parseInt() result. :)

[CODE="javascript"]function is_int(mixed_var)
{
return mixed_var === parseInt(mixed_var * 1);
}[/CODE]


Contribute a New function

More functions

In this category

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
var_dump
var_export

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: AYHAN BARI*, Nikita Ekshiyan, Nikita Ekshiyan, Petr Pavel, @HalfWinter, Paulo Freitas, Andros Peña Romo, @andorosu, Raimund Szabo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !