¿Cómo puedo redondear a números enteros en JavaScript?
Tengo el siguiente código para calcular un cierto porcentaje:
var x = 6.5;
var total;
total = x/15*100;
// Result 43.3333333333
Lo que quiero tener como resultado es el número exacto 43
y si el total es 43.5
debería redondearse a 44
¿Hay forma de hacer esto en JavaScript?
87
Author: John Washam, 2011-08-06
5 answers
Utilice el Math.round()
función para redondear el resultado al entero más cercano.
154
Author: Henning Makholm,
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-08-06 16:10:55
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-08-06 16:10:55
//method 1
Math.ceil(); // rounds up
Math.floor(); // rounds down
Math.round(); // does method 2 in 1 call
//method 2
var number = 1.5; //float
var a = parseInt(number); // to int
number -= a; // get numbers on right of decimal
if(number < 0.5) // if less than round down
round_down();
else // round up if more than
round_up();
Ya sea uno o una combinación resolverá su pregunta
56
Author: Drake,
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-08-06 16:10:37
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-08-06 16:10:37
total = Math.round(total);
Debería hacerlo.
10
Author: Phil,
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-08-06 16:13:26
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-08-06 16:13:26
Use Math.round
para redondear el número al entero más cercano:
total = Math.round(x/15*100);
7
Author: Gumbo,
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-08-06 16:10:41
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-08-06 16:10:41
Una solución muy sucinta para redondear un flotador x:
x = 0|x+0.5
O si solo desea floor su flotador
x = 0|x
Esto es un bit o con int 0, que elimina todos los valores después del decimal
3
Author: Karo Castro-Wunsch,
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
2016-08-04 18:12:42
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
2016-08-04 18:12:42