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.
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.
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.
Just do something like
value = value < 0 ? 0 : value;
or
if (value < 0) value = 0;
or
value = Math.max(0, value);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With