Solved

How to round to nearest 5

Posted on 2013-06-18
9
472 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 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 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
Technology Partners: 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 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 34

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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.

734 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