Categories
JavaScript Answers

How to increase the value of a number to the next multiple of 10, 100, 1000, 10,000 and so on with JavaScript?

Spread the love

You can achieve this by finding the next multiple of 10, 100, 1000, etc., by using simple mathematical operations in JavaScript.

We can do this with these functions:

function nextMultipleOfTen(number) {
    return Math.ceil(number / 10) * 10;
}

function nextMultipleOfHundred(number) {
    return Math.ceil(number / 100) * 100;
}

function nextMultipleOfThousand(number) {
    return Math.ceil(number / 1000) * 1000;
}

// Example usage
console.log(nextMultipleOfTen(25));      // Output: 30
console.log(nextMultipleOfHundred(123)); // Output: 200
console.log(nextMultipleOfThousand(5678)); // Output: 6000

In these functions, we divide the input number by the desired multiple (10, 100, or 1000).

We then use Math.ceil() to round the result up to the nearest integer.

Finally, we multiply this rounded result by the desired multiple to get the next multiple of 10, 100, or 1000.

We can use these functions to find the next multiple of any desired number.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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