Javascript Math Object Methods-negativos a cero


En Javascript parece que no puedo encontrar un método para establecer negativos a cero?

-90 se convierte en 0
-45 se convierte en 0
0 se convierte en 0
90 se convierte en 90

¿Hay algo así? Acabo de redondear los números.

Author: CharlesB, 2011-02-07

7 answers

Simplemente haz algo como

value = value < 0 ? 0 : value;

O

if (value < 0) value = 0;

O

value = Math.max(0, value);
 110
Author: aioobe,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-02-07 18:16:17

Supongo que podrías usar Math.max().

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

var num = -90;
num = Math.max(0,num); // 0
 36
Author: RightSaidFred,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-02-07 18:16:48

Si quieres ser inteligente:

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

Sin Embargo, Math.max o un operador condicional sería mucho más comprensible.
Además, esto tiene problemas de precisión para grandes números.

 6
Author: SLaks,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-02-07 18:18:05
Math.positive = function(num) {
  return Math.max(0, num);
}

// or 

Math.positive = function(num) {
  return num < 0 ? 0 : num;
}
 3
Author: Juan Mendes,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-02-07 21:14:34

x < 0 ? 0 : x hace el trabajo .

 2
Author: Alexandre C.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-02-07 18:16:51

No creo que tal función exista con el objeto Math nativo. Debe escribir un script para completar la función si necesita usarla.

 0
Author: jacobangel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-02-07 18:18:49

Recuerde el cero negativo.

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

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

 0
Author: Killy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-12 08:48:00