Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Last active March 15, 2021 03:15
Show Gist options
  • Save parzibyte/b2571ca62f91d75f21d35331a0b2e199 to your computer and use it in GitHub Desktop.
Save parzibyte/b2571ca62f91d75f21d35331a0b2e199 to your computer and use it in GitHub Desktop.
/*
https://parzibyte.me/blog
*/
const redondear = numero => {
// .5 o menos, baja. Si no, sube
const diferencia = numero - parseInt(numero);
if (diferencia <= 0.5) {
return Math.floor(numero);
} else {
return Math.ceil(numero);
}
}
console.log(redondear(9.5)); // 9
console.log(redondear(9.6)); // 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment