Javascript: Round up to the next multiple of 5
This will do the work:
function round5(x){ return Math.ceil(x/5)*5;}
It's just a variation of the common rounding number
to nearest multiple of x
function Math.round(number/x)*x
, but using .ceil
instead of .round
makes it always round up instead of down/up according to mathematical rules.
const roundToNearest5 = x => Math.round(x/5)*5
This will round the number to the nearest 5. To always round up to the nearest 5, use Math.ceil
. Likewise, to always round down, use Math.floor
instead of Math.round
.You can then call this function like you would any other. For example,
roundToNearest5(21)
will return:
20