1. ## Rounding Off (Urgent)

Hey people, I was just wondering if someone could help me with some code. I need to round a number off to the nearest multiple of 5 and i can't figure out how to do it. If you can help it would be much appreciated if you could tell me asap please. Thanks

2. Re: urgent

Think about how you'd do it without a computer. I'd probably divide the number by 5 and note the remainder. Then I'd subtract the number from the remainder.

3. writing out a plan on paper is key

this is how I just thought of

Code:
```#include<iostream>

int main()
{
int number = 13556;
int temp;
temp = number/5;
number = number - (number - temp*5);
std::cout << number;
}```

4. To shortern the code a bit.
Code:
```#include<iostream>

//13555
int main()
{
int number = 13556;
number -= number % 5;
std::cout << number;
}```

5. Thanks heaps, also does anyone know how to do it with a float? I need to take a float (number1) and round it off to the nearest multiple of 5 which will then be presented as number2.

6. That method will work for a float, just convert it to an int first (Since presumably you're not expecting any significant figures after the decimal point if it's a multiple of 5...)

7. I need to round a number off to the nearest multiple of 5 and i can't figure out how to do it.
Warning: neither of the posted methods will do what you said you wanted.

8. Originally Posted by 7stud
Warning: neither of the posted methods will do what you said you wanted.
True, to use a more 'mathematically correct' approach, an extra step is needed to check the halfway rounding point.
Code:
```#include <iostream>

int main()
{
const int number = 13;
const int multiple = 5;
int result=number-(number%multiple);

if ( (number%multiple) > ((float)multiple/2) )
result+=multiple;

std::cout << result;
std::cin.get();
}```

9. This will work too, 4 examples, two with int and two with float showing both rounding up and down

Code:
```#include <iostream>
int main()
{
int number = 13557;
std::cout << 5 * ((2 + number )/5 ) << "\n";

int number2 = 13558;
std::cout << 5 * ((2 + number2 )/5 ) << "\n";

float number3 = 13557.481;
std::cout << 5 * (int(2.5 + number3 )/5) << "\n";

float number4 = 13557.781;
std::cout << 5 * (int(2.5 + number4 )/5) << std::endl;

}```

10. How about some C++ casting syntax as well as code that won't produce cast warnings(VC++6)?

11. True, to use a more 'mathematically correct' approach,
I wasn't pointing out some mathematical technicality. 'round' has a common sense, intuitive meaning, and when someone asks you to round 14 to the nearest multiple of 5, they would not expect the result to be 10.

12. Originally Posted by 7stud
I wasn't pointing out some mathematical technicality. 'round' has a common sense, intuitive meaning, and when someone asks you to round 14 to the nearest multiple of 5, they would not expect the result to be 10.
I don't know. Are we just reading it differently?
Originally Posted by Razakel
I need to round a number off to the nearest multiple of 5 and i can't figure out how to do it.
'round off' also has a common sense, intuitive meaning.

13. 'round off' also has a common sense, intuitive meaning.
You could rephrase it in plenty of different ways, "round up", "round down", "round off", etc. I don't think the OP was quite expecting that sort of nitpicking though, but there's enough examples of both here. Whether he/she wants, for example, 19 to "rounded" to 20 or to 15.

How about some C++ casting syntax as well as code that won't produce cast warnings(VC++6)?
Fair comment, it was a bit of laziness. I'm not sure why you got the warnings (compiling in strict/pedantic mode?), both comeau and VC++2003 quietly accepted the C-cast anyway

Code:
```#include <iostream>

int main()
{
const int number = 13;
const int multiple = 5;
int result=number-(number%multiple);

if ( (number%multiple) > (static_cast<float>(multiple)/2.0) )
result+=multiple;

std::cout << result;
std::cin.get();
}```
Updated with C++ cast. Hopefully without warnings on VC6.

14. Originally Posted by pianorain
I don't know. Are we just reading it differently?'round off' also has a common sense, intuitive meaning.
First hit on google for 'round off':

Round off 39.18769 to the nearest thousandth.
39.18769 = 39.188
http://www.gomath.com/algebra/round.php

15. LoL...touche!