Thread: Rounding Off (Urgent)

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    2

    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. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    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.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    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. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    To shortern the code a bit.
    Code:
    #include<iostream>
    
    //13555
    int main()
    {
    	int number = 13556;
    	number -= number % 5;
    	std::cout << number;
    }

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    2
    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.
    Last edited by Razakel; 03-21-2006 at 09:18 PM.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    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. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote 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. #9
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    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;
    
    }
    Last edited by Darryl; 03-22-2006 at 09:49 AM.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    How about some C++ casting syntax as well as code that won't produce cast warnings(VC++6)?

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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.
    Last edited by 7stud; 03-22-2006 at 12:26 PM.

  12. #12
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote 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?
    Quote 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.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  13. #13
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    '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. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote 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
    Last edited by 7stud; 03-22-2006 at 05:00 PM.

  15. #15
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    LoL...touche!
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Minimax, URGENT :(
    By Dark-MX0Z in forum C++ Programming
    Replies: 6
    Last Post: 11-14-2007, 06:29 AM
  2. BitWise Newbie - Needs Urgent Help
    By frodonet in forum C Programming
    Replies: 15
    Last Post: 09-26-2007, 12:58 PM
  3. Newbie question: rounding numbers
    By cantore in forum C Programming
    Replies: 10
    Last Post: 02-04-2006, 03:24 PM
  4. Linked List Need Help Urgent
    By ykchua in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:57 PM
  5. Help with rounding a number
    By nickk in forum C Programming
    Replies: 3
    Last Post: 06-02-2004, 11:44 AM