JavaScript || How To Get The Next & Previous Multiple Of A Number

The following is a module with functions that demonstrates how to get the next and previous multiple of a number.

If a number is already a multiple, there is a parameter that allows you to specify if it should be rounded or not.

1. Get Next Multiple – Include Existing

The example below demonstrates how to round up a number to the next multiple. In this example, if a number is already a multiple, it will not be rounded up to the next multiple.

``` 1. Get Next - Include Existing JavaScript // Get next multiple - include if already a multiple. <script> (() => { let multiple = 5; for (let index = 1; index <= 10; ++index) { console.log(index, '=>', Utils.getNextMultiple(index, multiple)); } })(); </script> // expected output: /* 1 => 5 2 => 5 3 => 5 4 => 5 5 => 5 6 => 10 7 => 10 8 => 10 9 => 10 10 => 10 */ 123456789101112131415161718192021222324 // Get next multiple - include if already a multiple.  <script>(() => {     let multiple = 5;    for (let index = 1; index <= 10; ++index) {      console.log(index, '=>', Utils.getNextMultiple(index, multiple));    }   })();</script> // expected output: /*    1 => 5     2 => 5     3 => 5     4 => 5     5 => 5     6 => 10     7 => 10     8 => 10     9 => 10     10 => 10*/ ```

2. Get Next Multiple – Skip Existing

The example below demonstrates how to round up a number to the next multiple. In this example, if a number is already a multiple, it will be rounded up to the next multiple.

``` 2. Get Next - Skip Existing JavaScript // Get next multiple - skip if already a multiple. <script> (() => { let multiple = 5; // Set 3rd parameter to true to skip numbers that are already a // multiple to the next value for (let index = 1; index <= 10; ++index) { console.log(index, '=>', Utils.getNextMultiple(index, multiple, true)); } })(); </script> // expected output: /* 1 => 5 2 => 5 3 => 5 4 => 5 5 => 10 6 => 10 7 => 10 8 => 10 9 => 10 10 => 15 */ 123456789101112131415161718192021222324252627 // Get next multiple - skip if already a multiple.  <script>(() => {     let multiple = 5;     // Set 3rd parameter to true to skip numbers that are already a     // multiple to the next value    for (let index = 1; index <= 10; ++index) {      console.log(index, '=>', Utils.getNextMultiple(index, multiple, true));    }    })();</script> // expected output: /*    1 => 5     2 => 5     3 => 5      4 => 5     5 => 10      6 => 10      7 => 10      8 => 10      9 => 10      10 => 15*/ ```

3. Get Previous Multiple – Include Existing

The example below demonstrates how to round down a number to the previous multiple. In this example, if a number is already a multiple, it will not be rounded down to the previous multiple.

``` 3. Get Previous - Include Existing JavaScript // Get previous multiple - include if already a multiple. <script> (() => { let multiple = 5; for (let index = 10; index >= 1; --index) { console.log(index, '=>', Utils.getPreviousMultiple(index, multiple)); } })(); </script> // expected output: /* 10 => 10 9 => 5 8 => 5 7 => 5 6 => 5 5 => 5 4 => 0 3 => 0 2 => 0 1 => 0 */ 123456789101112131415161718192021222324 // Get previous multiple - include if already a multiple.  <script>(() => {     let multiple = 5;    for (let index = 10; index >= 1; --index) {      console.log(index, '=>', Utils.getPreviousMultiple(index, multiple));    }  })();</script> // expected output: /*    10 => 10     9 => 5     8 => 5     7 => 5     6 => 5     5 => 5     4 => 0     3 => 0     2 => 0     1 => 0*/ ```

4. Get Previous Multiple – Skip Existing

The example below demonstrates how to round down a number to the previous multiple. In this example, if a number is already a multiple, it will be rounded down to the previous multiple.

``` 4. Get Previous - Skip Existing JavaScript // Get previous multiple - skip if already a multiple. <script> (() => { let multiple = 5; // Set 3rd parameter to true to skip numbers that are already a // multiple to the previous value for (let index = 10; index >= 1; --index) { console.log(index, '=>', Utils.getPreviousMultiple(index, multiple, true)); } })(); </script> // expected output: /* 10 => 5 9 => 5 8 => 5 7 => 5 6 => 5 5 => 0 4 => 0 3 => 0 2 => 0 1 => 0 */ 123456789101112131415161718192021222324252627 // Get previous multiple - skip if already a multiple.  <script>(() => {     let multiple = 5;     // Set 3rd parameter to true to skip numbers that are already a     // multiple to the previous value    for (let index = 10; index >= 1; --index) {      console.log(index, '=>', Utils.getPreviousMultiple(index, multiple, true));    } })();</script> // expected output: /*    10 => 5     9 => 5     8 => 5      7 => 5     6 => 5     5 => 0     4 => 0     3 => 0     2 => 0     1 => 0*/ ```

5. Utils Namespace

The following is the Utils.js Namespace. Include this in your project to start using!

``` Utils.js Namespace JavaScript // ============================================================================ // Author: Kenneth Perkins // Date: Aug 8, 2020 // Taken From: http://programmingnotes.org/ // File: Utils.js // Description: Javascript that handles general utility functions // ============================================================================ /** * NAMESPACE: Utils * USE: Handles general utility functions. */ var Utils = Utils || {}; (function(namespace) { 'use strict'; // Property to hold public variables and functions let exposed = namespace; /** * FUNCTION: getNextMultiple * USE: Returns the next multiple of a given number. Unless specified, * it will not round up numbers which are already multiples. * @param number: The given number to find the next multiple * @param multiple: The multiple number * @param skipAlreadyMultiple: Boolean that specifies if input numbers which * are already multiples should be skipped to the next multiple or not * @return: The next multiple of the given number. */ exposed.getNextMultiple = (number, multiple, skipAlreadyMultiple = false) => { let retVal = 0; if (multiple !== 0) { let remainder = (number % multiple); if (!skipAlreadyMultiple && remainder === 0) { retVal = number; } else { retVal = number + (multiple - remainder); } } return retVal; } /** * FUNCTION: getPreviousMultiple * USE: Returns the previous multiple of a given number. Unless specified, * it will not round down numbers which are already multiples. * @param number: The given number to find the previous multiple * @param multiple: The multiple number * @param skipAlreadyMultiple: Boolean that specifies if input numbers which * are already multiples should be skipped to the previous multiple or not. * @return: The previous multiple of the given number. */ exposed.getPreviousMultiple = (number, multiple, skipAlreadyMultiple = false) => { return exposed.getNextMultiple(number, multiple, !skipAlreadyMultiple) - multiple; } (function (factory) { if (typeof define === 'function' && define.amd) { define([], factory); } else if (typeof exports === 'object') { module.exports = factory(); } }(function() { return namespace; })); }(Utils)); // http://programmingnotes.org/ 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 // ============================================================================//    Author: Kenneth Perkins//    Date:   Aug 8, 2020//    Taken From: http://programmingnotes.org///    File:  Utils.js//    Description: Javascript that handles general utility functions    // ============================================================================/*** NAMESPACE: Utils* USE: Handles general utility functions.*/var Utils = Utils || {};(function(namespace) {    'use strict';       // Property to hold public variables and functions    let exposed = namespace;        /**    * FUNCTION: getNextMultiple    * USE: Returns the next multiple of a given number. Unless specified,     *   it will not round up numbers which are already multiples.    * @param number: The given number to find the next multiple    * @param multiple: The multiple number     * @param skipAlreadyMultiple: Boolean that specifies if input numbers which     *   are already multiples should be skipped to the next multiple or not    * @return: The next multiple of the given number.    */      exposed.getNextMultiple = (number, multiple, skipAlreadyMultiple = false) => {        let retVal = 0;        if (multiple !== 0) {            let remainder = (number % multiple);            if (!skipAlreadyMultiple && remainder === 0) {                retVal = number;            } else {                retVal = number + (multiple - remainder);            }                    }        return retVal;    }        /**    * FUNCTION: getPreviousMultiple    * USE: Returns the previous multiple of a given number. Unless specified,     *   it will not round down numbers which are already multiples.    * @param number: The given number to find the previous multiple    * @param multiple: The multiple number     * @param skipAlreadyMultiple: Boolean that specifies if input numbers which     *   are already multiples should be skipped to the previous multiple or not.    * @return: The previous multiple of the given number.    */    exposed.getPreviousMultiple = (number, multiple, skipAlreadyMultiple = false) => {        return exposed.getNextMultiple(number, multiple, !skipAlreadyMultiple) - multiple;    }     (function (factory) {        if (typeof define === 'function' && define.amd) {            define([], factory);        } else if (typeof exports === 'object') {            module.exports = factory();        }    }(function() { return namespace; })); }(Utils)); // http://programmingnotes.org/ ```

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.