**New course!**Every coder should learn

**Generative AI!**

0

# Round a number

On JavaScript: I need to perform some calculations on a rounded number that the user inputs. This number has to be rounded to the next multiple of five: For example: If user inputs 25, the rounded number should stay 25. If user inputs 26, the rounded number should be 30. If users inputs 29, the rounded number should be 30. How can I do this?

9 Answers

+ 2

Daniel Lopez
Try this
x = 29
console.log(Math.ceil(x / 5) * 5)
Ipang I think he want round off next to 5

+ 1

Use this formula:
a = 20
i = a%5==0 ? a/5*5 : Math.floor(a/5)*5+5
console.log(i)

+ 1

Daniel Lopez
Yes what is wrong in Carlos . His solution is also working as you want.

0

Doesn’t work

0

What happens when input is 21, 22, 23 or 24? does it round down to 20?

0

Daniel Lopez can you elaborate where it doesn't work?

0

Carlos the solution I’m looking for is to understand how many people do I need to recruit to form groups of five.
So if user inputs 24 people, I will only be able to form 4 groups of five people (=20) and will have a remainder of 4 people.
So the program I am writting should give me as output the number of people missing that I need to form groups with no remainder.
If the user inputs 24, my console.log should output 1...because I need to get another person to form groups of five people.
Your formula outputs another different number

0

Still no right answer!

0

This is my code.
function main() {
var numberVolunteers = parseInt(readLine(), 10)
// Your code here
var numberTotal= (numberVolunteers % 5 == 0) ? ((numberVolunteers/5*5)-numberVolunteers): Math.ceil (((numberVolunteers/5)*5+5)-numberVolunteers)
console.log (numberTotal)
}
When the input has the first option, ir works. On the seccond option I need to output the result of the substraction of the next rounded multiple of 5, minus the user input