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?

30th Mar 2021, 4:24 AM
Daniel Lopez
9 ответов
+ 2
Daniel Lopez Try this x = 29 console.log(Math.ceil(x / 5) * 5) Ipang I think he want round off next to 5
30th Mar 2021, 5:59 AM
A͢J
A͢J - avatar
+ 1
Use this formula: a = 20 i = a%5==0 ? a/5*5 : Math.floor(a/5)*5+5 console.log(i)
30th Mar 2021, 5:23 AM
Carlos
Carlos - avatar
+ 1
Daniel Lopez Yes what is wrong in Carlos . His solution is also working as you want.
30th Mar 2021, 6:12 AM
A͢J
A͢J - avatar
0
Doesn’t work
30th Mar 2021, 5:30 AM
Daniel Lopez
0
What happens when input is 21, 22, 23 or 24? does it round down to 20?
30th Mar 2021, 5:37 AM
Ipang
0
Daniel Lopez can you elaborate where it doesn't work?
30th Mar 2021, 5:47 AM
Carlos
Carlos - avatar
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
2nd Apr 2021, 4:06 AM
Daniel Lopez
0
Still no right answer!
5th Apr 2021, 2:05 AM
Daniel Lopez
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
5th Apr 2021, 2:07 AM
Daniel Lopez