- Related Questions & Answers
- How to round the decimal number to the nearest tenth in JavaScript?
- How to round to the nearest hundred in R?
- How to round up a number in JavaScript?
- How to round down to nearest integer in MySQL?
- Round number down to nearest power of 10 JavaScript
- Round seconds to nearest half minute in MySQL?
- Round a number to the nearest even number in C#
- Summing up digits and finding nearest prime in JavaScript
- How to round a number to n decimal places in Java
- How to free up the memory in JavaScript?
- How do you round up a float number in Python?
- Nearest Prime to a number - JavaScript
- Print the nearest prime number formed by adding prime numbers to N
- Nearest palindrome in JavaScript
- How to get the value of a number rounded to the nearest integer in JavaScript?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a number,

const num = 76;

However,

If we round off this number to nearest 10 place, the result will be 80

If we round off this number to nearest 100 place, the result will be 100

If we round off this number to nearest 1000 place, the result will be 0

We are required to write a JavaScript function that takes in a number to be rounded as the first argument and the rounding off factor as the second argument.

The function should return the result after rounding off the number.

The code for this will be −

const num = 76; const roundOffTo = (num, factor = 1) => { const quotient = num / factor; const res = Math.round(quotient) * factor; return res; }; console.log(roundOffTo(num, 10)); console.log(roundOffTo(num, 100)); console.log(roundOffTo(num, 1000));

And the output in the console will be −

80 100 0

Advertisements