How to check the value given is a positive or negative integer?
if (values > 0) {
// Do Something
}
Am I the only one who read this and realized that none of the answers addressed the "integer" part of the question?
The problem
var myInteger = 6;
var myFloat = 6.2;
if( myInteger > 0 )
// Cool, we correctly identified this as a positive integer
if( myFloat > 0 )
// Oh, no! That's not an integer!
The solution
To guarantee that you're dealing with an integer, you want to cast your value to an integer then compare it with itself.
if( parseInt( myInteger ) == myInteger && myInteger > 0 )
// myInteger is an integer AND it's positive
if( parseInt( myFloat ) == myFloat && myFloat > 0 )
// myFloat is NOT an integer, so parseInt(myFloat) != myFloat
Some neat optimizations
As a bonus, there are some shortcuts for converting from a float to an integer in JavaScript. In JavaScript, all bitwise operators (|
, ^
, &
, etc) will cast your number to an integer before operating. I assume this is because 99% of developers don't know the IEEE floating point standard and would get horribly confused when "200 | 2" evaluated to 400(ish). These shortcuts tend to run faster than Math.floor
or parseInt
, and they take up fewer bytes if you're trying to eke out the smallest possible code:
if( myInteger | 0 == myInteger && myInteger > 0 )
// Woot!
if( myFloat | 0 == myFloat && myFloat > 0 )
// Woot, again!
But wait, there's more!
These bitwise operators are working on 32-bit signed integers. This means the highest bit is the sign bit. By forcing the sign bit to zero your number will remain unchanged only if it was positive. You can use this to check for positiveness AND integerness in a single blow:
// Where 2147483647 = 01111111111111111111111111111111 in binary
if( (myInteger & 2147483647) == myInteger )
// myInteger is BOTH positive and an integer
if( (myFloat & 2147483647) == myFloat )
// Won't happen
* note bit AND operation is wrapped with parenthesis to make it work in chrome (console)
If you have trouble remembering this convoluted number, you can also calculate it before-hand as such:
var specialNumber = ~(1 << 31);
Checking for negatives
Per @Reinsbrain's comment, a similar bitwise hack can be used to check for a negative integer. In a negative number, we do want the left-most bit to be a 1, so by forcing this bit to 1 the number will only remain unchanged if it was negative to begin with:
// Where -2147483648 = 10000000000000000000000000000000 in binary
if( (myInteger | -2147483648) == myInteger )
// myInteger is BOTH negative and an integer
if( (myFloat | -2147483648) == myFloat )
// Won't happen
This special number is even easier to calculate:
var specialNumber = 1 << 31;
Edge cases
As mentioned earlier, since JavaScript bitwise operators convert to 32-bit integers, numbers which don't fit in 32 bits (greater than ~2 billion) will fail
You can fall back to the longer solution for these:
if( parseInt(123456789000) == 123456789000 && 123456789000 > 0 )
However even this solution fails at some point, because parseInt
is limited in its accuracy for large numbers. Try the following and see what happens:
parseInt(123123123123123123123); // That's 7 "123"s
On my computer, in Chrome console, this outputs: 123123123123123130000
The reason for this is that parseInt treats the input like a 64-bit IEEE float. This provides only 52 bits for the mantissa, meaning a maximum value of ~4.5e15 before it starts rounding
To just check, this is the fastest way, it seems:
var sign = number > 0 ? 1 : number == 0 ? 0 : -1;
//Is "number": greater than zero? Yes? Return 1 to "sign".
//Otherwise, does "number" equal zero? Yes? Return 0 to "sign".
//Otherwise, return -1 to "sign".
It tells you if the sign is positive (returns 1), or equal to zero (returns 0), and otherwise (returns -1). This is a good solution because 0 is not positive, and it is not negative, but it may be your var.
Failed attempt:
var sign = number > 0 ? 1 : -1;
...will count 0 as a negative integer, which is wrong.
If you're trying to set up conditionals, you can adjust accordingly. Here's are two analogous example of an if/else-if statement:
Example 1:
number = prompt("Pick a number?");
if (number > 0){
alert("Oh baby, your number is so big!");}
else if (number == 0){
alert("Hey, there's nothing there!");}
else{
alert("Wow, that thing's so small it might be negative!");}
Example 2:
number = prompt("Pick a number?");
var sign = number > 0 ? 1 : number == 0 ? 0 : -1;
if (sign == 1){
alert("Oh baby, your number is so big!" + " " + number);}
else if (sign == 0){
alert("Hey, there's nothing there!" + " " + number);}
else if (sign == -1){
alert("Wow, that thing's so small it might be negative!" + " " + number);}
I thought here you wanted to do the action if it is positive.
Then would suggest:
if (Math.sign(number_to_test) === 1) {
function_to_run_when_positive();
}