Thread: round a number to superior integer

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    42

    round a number to superior integer

    Don't know if I said that right but well...

    Code:
    number = 42
    
    //what I want to is to bring number to 50 
    if number would have been 23 --> 30
    19-->20
    60-->60
    thanks
    Last edited by nevrax; 03-29-2007 at 11:00 PM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Think it through. You're doing this based off of being divisible by 10. Hence, find out if the number is divisible by 10... ie. num % 10.

    If the result of that is not zero, that means you want to round it up. So how much do you want to round it by? 42 -> 50, 23 -> 30, 19 -> 20, and 60 -> 60. Notice the amount in each and every case?

    Just add that amount to the original number.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    42
    it's random number, these numbers were just example...but I think I've got it
    Last edited by nevrax; 03-29-2007 at 11:18 PM.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    It doesn't matter what the number itself is. I was trying to give you a forumula to do it. BTW, it's only one line of C code to do what you're asking to do.

  5. #5
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    something along the lines of:

    Code:
    rounded_number = (number % ... != ... ) ? ((number / ...) + ...) * ... : number

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by KONI View Post
    something along the lines of:

    Code:
    rounded_number = (number % ... != ... ) ? ((number / ...) + ...) * ... : number
    You way overcomplicated it.

    Well, technically anyway. You don't really need the condition if you want to be lazy.
    Last edited by MacGyver; 03-30-2007 at 03:09 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-19-2009, 07:19 PM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. Contest - Traveller's Dilemma
    By pianorain in forum Contests Board
    Replies: 49
    Last Post: 07-03-2007, 08:39 AM
  4. Perfect number...
    By Argo_Jeude in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 01:53 PM
  5. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM