# How to round to nearest 5

This is proving more difficult than I expected.

How do I round an integer up to the nearest '5'?

346 = 350

123 = 125

876 = 880

etc.

I tried ceil , but can't get it to produce correct result.

Thank you.
LVL 1
###### Who is Participating?

x

IT Business Systems Analyst / Software DeveloperCommented:
``````int num = 123;

num = ((int) ((num + 4) / 5)) * 5;
``````
0

Commented:
(x + 4) / 5 * 5
0

Commented:
the 'nearest' 5 of 346 is 345 and not 350.

Sara
0

IT Business Systems Analyst / Software DeveloperCommented:
@Sara,

But the question author did say, "How do I round an integer up to the nearest '5'?"

Easily missed though! ;)
0

Commented:
If you need it to work with both positive and negative ints
x+(-x%5+5)%5
0

Commented:
yes. i didn't read the 'up'. :-)

Sara
0

IT Business Systems Analyst / Software DeveloperCommented:
@Sara, I have actually just now read the title of the question where the author HASN'T used the word 'up' so yes, that does leave it not 100% clear what the author was after! ;)
0

Author Commented:
I did mean round up. thank you everyone. it looks so easy once I study your code!
0

IT Business Systems Analyst / Software DeveloperCommented:
Not a problem, glad to help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.