CKTIS / JavaScript
Math.round: entero más cercano
Math.round convierte su argumento a Number y devuelve el Number entero más cercano, resolviendo empates hacia +Infinity.
Versión: ECMAScript 1 onward; example target: Node.js 24.22.2
Sintaxis
Math.round(x)Math.round(x) aplica ToNumber. Si dos enteros están igual de cerca, elige el más próximo a +Infinity: 2.5 da 3, pero -2.5 da -2. Un argumento ya entero, NaN, +Infinity, -Infinity, +0 o -0 se devuelve conservando ese valor especial.
Desde -0.5 inclusive hasta 0 exclusivo, el resultado es -0; se detecta con Object.is o el recíproco. Por eso Math.round(x) no siempre equivale a Math.floor(x + 0.5), que puede perder el signo del cero, y la suma también puede redondear un x muy grande.
Una cadena numérica se convierte, la falta de argumento da NaN, y Symbol o BigInt lanza TypeError. La función existe desde ECMAScript 1 y no es constructora. Redondea valores, no formatea texto; presenta decimales aparte considerando el redondeo binary64.
Ejemplos
console.log([2.3, 2.5, -2.5, -2.6].map(Math.round).join(","));
console.log(Object.is(Math.round(-0.5), -0), Object.is(Math.round(-0.1), -0));
console.log(Math.round("4.6"), Math.round(Infinity));
console.log(Number.isNaN(Math.round()), Object.is(Math.round(-0), -0));
try { Math.round(3n); } catch (error) { console.log(error instanceof TypeError); }Resultado
2,3,-2,-3 true true 5 Infinity true true true
Math.round → ties toward +Infinity and preserved -0