Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Math Object Methods - negatives to zero

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.

like image 737
FFish Avatar asked Feb 07 '11 18:02

FFish


People also ask

Why does JavaScript have negative zero?

This is because JavaScript implements the IEEE Standard for Floating-Point Arithmetic (IEEE 754), which has signed zeroes. Here is how Wikipedia explains signed zeroes: “Signed zero is zero with an associated sign. In ordinary arithmetic, the number 0 does not have a sign, so that −0, +0 and 0 are identical.

How do you pass negative values in JavaScript?

To use negative numbers, just place a minus (-) character before the number we want to turn into a negative value: let temperature = -42; What we've seen in this section makes up the bulk of how we will actually use numbers.


1 Answers

Just do something like

value = value < 0 ? 0 : value; 

or

if (value < 0) value = 0; 

or

value = Math.max(0, value); 
like image 190
aioobe Avatar answered Sep 22 '22 17:09

aioobe