Solved

How to round to nearest 5

Posted on 2013-06-18
9
466 Views
Last Modified: 2013-06-22
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.
0
Comment
Question by:edvinson
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39258409
(x + 4) / 5 * 5
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 39258412
int num = 123;

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

Open in new window

0
 
LVL 33

Expert Comment

by:sarabande
ID: 39258522
the 'nearest' 5 of 346 is 345 and not 350.

add 2 to the number (instead of 4) if you agree.

Sara
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 35

Expert Comment

by:mccarl
ID: 39258530
@Sara,

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

Easily missed though! ;)
0
 
LVL 84

Expert Comment

by:ozo
ID: 39258568
If you need it to work with both positive and negative ints
x+(-x%5+5)%5
0
 
LVL 33

Expert Comment

by:sarabande
ID: 39258660
yes. i didn't read the 'up'. :-)

Sara
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39259590
@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
 
LVL 1

Author Comment

by:edvinson
ID: 39267624
I did mean round up. thank you everyone. it looks so easy once I study your code!
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39267684
Not a problem, glad to help!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ Class Serialization, Encapsulation and Formatted Output 8 80
passing data from one form to another form in c++ 27 83
designing in object programming 12 95
C++ Code Issue 4 26
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question