111

in Javascript I can't seem to find a method to set negatives to zero?

-90 becomes 0
-45 becomes 0
0 becomes 0
90 becomes 90

Is there anything like that? I have just rounded numbers.

CharlesB
  • 86,532
  • 28
  • 194
  • 218
FFish
  • 10,964
  • 34
  • 95
  • 136

8 Answers8

245

Just do something like

value = value < 0 ? 0 : value;

or

if (value < 0) value = 0;

or

value = Math.max(0, value);
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 2
    Quick JSPerf shows value = value < 0 ? 0 : value; is on top http://jsperf.com/negatives-to-zero – Skeep Aug 19 '14 at 08:20
  • 42
    I like the `Math.max` one the most, because it only requires referencing `value` once – Matt Fletcher Sep 15 '17 at 11:05
  • In a more fair benchmark, Math.max wins probably because it was already instantiated once. https://jsbench.me/tbkmm8varf/1 – insign Mar 23 '21 at 16:41
  • 1
    @insign - I'm getting different results every time I run your benchmark - sometimes a 3 way tie. Seems like readability and consistency are more important factors if the margins are so close between them. – squarecandy Oct 14 '21 at 03:59
78

I suppose you could use Math.max().

var num = 90;
num = Math.max(0,num) || 0; // 90

var num = -90;
num = Math.max(0,num) || 0; // 0
SuperStormer
  • 4,997
  • 5
  • 25
  • 35
RightSaidFred
  • 11,209
  • 35
  • 35
  • 3
    This one should be the accepted answer in my opinion. Just saying... :) – ksugiarto Jun 30 '15 at 07:28
  • 4
    One thing to keep in mind is that both `Math.max(0, NaN)` and `Math.max(0, undefined)` return `NaN` so you may want to do something along these lines: `Math.max(0, num) || 0` – jwerre Jan 09 '19 at 21:11
  • What's the `~~`? – RedGuy11 Mar 21 '22 at 16:57
  • 1
    @RedGuy11 The `~~` is obstrusive and wrong, it converts it to binary. Which in turn makes it work ONLY for integers: `~~.1 == 0` Not sure why this is upvoted so often, no one sees how wrong it is? – kungfooman May 29 '22 at 09:49
8

If you want to be clever:

num = (num + Math.abs(num)) / 2;

However, Math.max or a conditional operator would be much more understandable.
Also, this has precision issues for large numbers.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
4
function makeNegativeNumberZero(num) {
  return !!Math.max(0, num);
}

// or 

function makeNegativeNumberZero(num) {
  return num < 0 ? 0 : num;
}
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • 1
    zero is neither positive nor negative, your function should be renamed according to that – Killy Jan 15 '18 at 15:17
  • 1
    I wouldn't recommend extending a native object. it's a bad practice. refer to https://stackoverflow.com/a/14034242/11877987 – Ammar Oker Jul 07 '22 at 20:10
3

x < 0 ? 0 : x does the job .

Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
0

Remember the negative zero.

function isNegativeFails(n) {
    return n < 0;
}
function isNegative(n) {
    return ((n = +n) || 1 / n) < 0;
}
isNegativeFails(-0); // false
isNegative(-0); // true
Math.max(-0, 0); // 0
Math.min(-0, 0); // -0

Source: http://cwestblog.com/2014/02/25/javascript-testing-for-negative-zero/

Killy
  • 300
  • 5
  • 13
0

Well value = Math.max(0,value) is just neat but after 10 years, i just don't want one other nice method to go unmentioned.

value < 0 && (value = 0);
Redu
  • 25,060
  • 6
  • 56
  • 76
0
var num = 90;
num = Math.max(0,num); // 90

var num = -90;
num =Math.max(0,num); //0

This method can be used

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Test
  • 1
  • 1
    Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you’ve made. – jasie Jun 27 '22 at 06:20