Ayfri
03/11/2022, 4:34 AMround(number / length) * length
to round a number by a length ?Michael de Kaste
03/11/2022, 8:44 AM"%.${length}f".format(number)
otherwise, mathmatically theres is not really much you can do to round more efficientlyKlitos Kyriacou
03/11/2022, 9:20 AMlength
). I think, if length
is an integer, you can probably make it slightly more efficient using just integer arithmetic, but it's probably not worth the effort.Ayfri
03/11/2022, 11:38 AMKlitos Kyriacou
03/11/2022, 12:32 PMTim Oltjenbruns
03/11/2022, 6:34 PMconst numberToRound = 15;
const length = 8
const bitmask = Number.MAX_SAFE_INTEGER - length + 1;
console.log(numberToRound & bitmask);
But again, only works for multiples of 2