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; } |
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.
@ 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 ; )
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.
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.
@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!
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.
@ 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.
This version will also work with strings
function is_int(value){
if(parseFloat(value) == parseInt(value)){
return true;
} else {
return false;
}
}
@ 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. ;/
@ 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!


CoursesWeb
Apr 30th
For is_int i use this version:
function is_int(n) { return typeof(n)==="number" && Math.round(n) == n; }