Solved

How to round to nearest 5

Posted on 2013-06-18
9
461 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
Comment Utility
(x + 4) / 5 * 5
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
Comment Utility
int num = 123;

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

Open in new window

0
 
LVL 32

Expert Comment

by:sarabande
Comment Utility
the 'nearest' 5 of 346 is 345 and not 350.

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

Sara
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
@Sara,

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

Easily missed though! ;)
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 84

Expert Comment

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

Expert Comment

by:sarabande
Comment Utility
yes. i didn't read the 'up'. :-)

Sara
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
@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
Comment Utility
I did mean round up. thank you everyone. it looks so easy once I study your code!
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
Not a problem, glad to help!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now