Open In App

Round off a number to the next multiple of 5 using JavaScript

In this article, we are given a positive integer n and the task is to round the number to the next whole number divisible by 5.  There are various methods to Round off a number to the next multiple of 5 using JavaScript:

Examples:



Input : 46 
Output : 50

Input : 21
Output : 25

Input : 30 
Output : 30

Approach 1:

Example:






<script>
    function round(x) {
        return Math.ceil(x / 5) * 5;
    }
      
    var n = 34;
    console.log(round(n));
</script>

Output:

35

Approach 2:

Example:




<script>
    function round(x) {
        if (x % 5 == 0) {
            return (Math.floor(x / 5)) * 5;
        } else {
            return ((Math.floor(x / 5)) * 5) + 5;
        }
    }
      
    var n = 34;
    console.log(round(n));
</script>

Output:

35

Article Tags :