Skip to content

Round a decimal number to nearest decimal that is divisible by 5 in JavaScript

I would like to convert 1.2 to 1.5 and 1.7 to 2. This is basically rounding a decimal number to nearest decimal that is divisible by 5.

const roundToNearest5 = x => Math.ceil(x)-x > 0.5 ? Math.ceil(x)-0.5: Math.ceil(x);
console.log(roundToNearest5(1.2))
console.log(roundToNearest5(1.4))
console.log(roundToNearest5(1.7))
See also  How to handle paste event in textarea HTML element?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.