How to check a given value - a positive or negative integer?

Suppose I have a value of 10 assigned to a variable;

var values = 10; 

and I want to run a specific function if the value is positive

 if(values = +integer){ //do something with positive } else { //do something with negative values } 

How will this be achieved?

+58
javascript jquery
Aug 12 2018-11-11T00:
source share
16 answers
 if (values > 0) { // Do Something } 
+141
Aug 12
source share

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 could be your var.

Unsuccessful attempt:

 var sign = number > 0 ? 1 : -1; 

... will count 0 as a negative integer , which is not true.

If you are trying to customize the legend, you can customize it accordingly. Here are two similar examples of the 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 nothing there!");} else{ alert("Wow, that thing 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 nothing there!" + " " + number);} else if (sign == -1){ alert("Wow, that thing so small it might be negative!" + " " + number);} 
+19
Oct 17 2018-11-11T00:
source share

Am I the only one who read this and realized that none of the answers are addressed to the "integer" part of the question?

This 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 not an integer! 

Decision

To ensure that you are dealing with an integer, you want to bring your value to an integer, and then compare it with yourself.

 if( parseInt( myInteger ) == myInteger && myInteger > 0 ) // myInteger is an integer AND it positive if( parseInt( myFloat ) == myFloat && myFloat > 0 ) // myFloat is NOT an integer, so parseInt(myFloat) != myFloat 

Some neat optimizations

As a bonus, there are several shortcuts for converting from a floating point number to an integer in JavaScript . In JavaScript, all bitwise operators ( | , ^ , & etc.) will convert your number to an integer before starting work. I guess this is because 99% of developers do not know the IEEE floating point standard and will be terribly confused when "200 | 2" evaluates to 400 (ish). These shortcuts tend to be faster than Math.floor or parseInt , and take up less bytes if you are trying to extract the smallest code possible:

 if( myInteger | 0 == myInteger && myInteger > 0 ) // Woot! if( myFloat | 0 == myFloat && myFloat > 0 ) // Woot, again! 

But wait, there is more!

These bitwise operators work with 32-bit signed integers. This means that the most significant bit is a sign bit. If you set the sign bit to zero, your number will remain unchanged only if it is positive . You can use this to test positivity and integerity in one hit:

 // 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 confusing number, you can also calculate it in advance:

 var specialNumber = ~(1 << 31); 

Negative Check

According to a comment by @Reinsbrain, a similar bitwise hack can be used to check for a negative integer. In a negative number, we want the left bit to be 1, therefore, forcing this bit to 1, then the number will only remain unchanged if it was negative, to start 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 are converted to 32-bit integers, numbers that do not fit into 32-bit (more than ~ 2 billion) will fail

You can return to a longer solution for these:

 if( parseInt(123456789000) == 123456789000 && 123456789000 > 0 ) 

However, even this solution fails at some point, because parseInt limited in its accuracy for large numbers. Try the following and see what happens:

 parseInt(123123123123123123123); // That 7 "123"s 

On my computer in the Chrome console, this outputs: 123123123123123130000

The reason for this is that parseInt treats the input as a 64-bit floating-point IEEE value. This provides only 52 bits for the mantissa, which means a maximum value of ~ 4.5e15 before rounding begins

+14
Jul 10 '14 at 16:23
source share

I thought you want to do an action if it is positive.

Then they would offer:

 if (Math.sign(number_to_test) === 1) { function_to_run_when_positive(); } 
+8
Feb 13 '17 at 15:58
source share

1 Checking a positive value

In a simple javascript comparison, for example: the value> == 0 does not give us an answer due to the existence of -0 and +0 (This concept has roots in derived equations) Below is an example of these values ​​and its properties:

 var negativeZero = -0; var negativeZero = -1 / Number.POSITIVE_INFINITY; var negativeZero = -Number.MIN_VALUE / Number.POSITIVE_INFINITY; var positiveZero = 0; var positiveZero = 1 / Number.POSITIVE_INFINITY; var positiveZero = Number.MIN_VALUE / Number.POSITIVE_INFINITY; -0 === +0 // true 1 / -0 // -Infinity +0 / -0 // NaN -0 * Number.POSITIVE_INFINITY // NaN 

With this in mind, we can write a function similar to the one below to check the sign of a given number:

 function isPositive (number) { if ( number > 0 ) { return true; } if (number < 0) { return false; } if ( 1 / number === Number.POSITIVE_INFINITY ) { return true; } return false; } 

2a Checking a number that is an integer (in a mathematical sense)

To verify that the number is an integer, we can use the following function:

 function isInteger (number) { return parseInt(number) === number; } //* in ECMA Script 6 use Number.isInteger 

2b Checking a number that is an integer (in computer science)

In this case, we check that the number does not have an exponential part (note that in JS numbers are presented in floating point format with double precision) However, in javascript it is more convenient to check that the value is "safe integer" ( http: // people. mozilla.org/~jorendorff/es6-draft.html#sec-number.max_safe_integer ) - to make it simple, this means that we can add / subtract 1 to the "safe integer" and make sure the result is the same as expected from math lessons. To illustrate what I mean, the result of some unsafe actions:

 Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2; // true Number.MAX_SAFE_INTEGER * 2 + 1 === Number.MAX_SAFE_INTEGER * 2 + 4; // true 

Well, therefore, to verify that the number is a safe integer, we can use Number.MAX_SAFE_INTEGER / Number.MIN_SAFE_INTEGER and parseInt to ensure that the number is an integer.

 function isSafeInteger (number) { return parseInt(number) === number && number <== Number.MAX_SAFE_INTEGER && number >== Number.MIN_SAFE_INTEGER } //* in ECMA Script 6 use Number.isSafeInteger 
+6
May 24 '15 at 12:28
source share
 if ( values > 0 ) { // Yeah, it positive } 
+5
Aug 12
source share

just write:

 if(values > 0){ //positive } else{ //negative } 
+5
Aug 12
source share
 if(values >= 0) { // as zero is more likely positive than negative } else { } 
+5
Aug 12 2018-11-11T00:
source share
 if ( values > 0 ) { //you got a positive value }else{ //you got a negative or zero value } 
+4
Aug 12 2018-11-11T00:
source share

To check the number, a positive, negative, or negative zero. Check its sign using the Math.sign () method, which will give you -1, - 0,0, and 1 based on positive negative and negative zero or zero.

  Math.sign(-3) // -1 Math.sign(3) // 1 Math.sign(-0) // -0 Math.sign(0) // 0 
+2
Mar 06 '18 at 11:23
source share

Positive number:

 if (parseInt(values, 10) > 0) { } 
+1
Feb 07 '19 at 11:20
source share

I use in this case and it works :)

 var pos = 0; var sign = 0; var zero = 0; var neg = 0; for( var i in arr ) { sign = arr[i] > 0 ? 1 : arr[i] == 0 ? 0 : -1; if (sign === 0) { zero++; } else if (sign === 1 ) { pos++; } else { neg++; } } 
0
Mar 09 '16 at 2:31
source share

First you need to check if the input interger value is isNumeric () function. Then add a condition or greater than 0. This is jQuery code for it.

 function isPositiveInteger(n) { return ($.isNumeric(n) && (n > 0)); } 
0
Jan 20 '17 at 18:45
source share

Math.abs() resulting value is a number, not a string, how about using Math.abs() ? This native JavaScript function returns the absolute value of a number:

 Math.abs(-1) // 1 

So you can use it like this:

 var a = -1; if(a == Math.abs(a)){ // false } var b = 1; if(b == Math.abs(b)){ // true } 
0
Nov 27 '18 at 16:14
source share

I know it has been a while, but there is a more elegant solution. From the Mozilla documentation :

 Math.sign(parseInt(-3)) 

This will give you -1 for negative, 0 for zero and 1 for positive.

0
Feb 07 '19 at 11:16
source share

To check for a positive integer:

 var isPositiveInteger = function(n) { return ($.isNumeric(n)) && (Math.floor(n) == n) && (n > 0); } 
-one
Jun 08 '17 at 6:09
source share



All Articles