Solved

How to round to nearest 5

Posted on 2013-06-18
9
474 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 84

Expert Comment

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

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 34

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 36

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 34

Expert Comment

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

Sara
0
 
LVL 36

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 36

Expert Comment

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

623 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