0

Let's say I have a number

let x = 890

and I want it to be rounded to 900

and if the number is 820 then it should be rounded to 800.

Let's also assume the input number (x) will always be an integer,

I know that we can solve this by manually checking if it is greater than 50 or not, but is there a simpler solution? preferably by recursion in javascript.

1
  • 6
    Math.round(x / 100) * 100 Commented Sep 2, 2021 at 12:18

2 Answers 2

3

Just of the top of my head:

Take any number, divide it by hundred, round the number with Math.round(), multiply by 100 to get your desired number.

const roundNumber = (num) => Math.round(num/100) *100
Sign up to request clarification or add additional context in comments.

Comments

2

If you want to round up and down.

Math.round(number/100)*100

For more indepth and also a comparison with Math.ceil() check out this link:

How to round up to the nearest 100 in JavaScript

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.