return value to nearest 100?

This is a discussion on return value to nearest 100? within the C# Programming forums, part of the General Programming Boards category; Does anyone know an algorithm to return any number to the nearest 100? eg. I pass in 2733, algorithm returns ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    38

    return value to nearest 100?

    Does anyone know an algorithm to return any number to the nearest 100?

    eg. I pass in 2733, algorithm returns 2700.

    4599.....returns 4600

    12....returns 0

    98....returns 100


    etc

    many thanks in advance.

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    Something along the lines of

    Code:
    int toNearest = 100;
    
    int value = 2175;
    int rest = value % toNearest;
    
    //check if rest is > toNearest/2 
      if so => int nearest = (value-rest)+toNearest
    else 
     int nearest = (value-rest)
    I think something would do what you want to do, now its your turn to change it in some nice looking C# code , also if you turn this into somesort of method, dont forget the case that if you want: nearest 100, and value = 550 , that it will go to the lowerboundary or the upperboundary ( adding a bool somewhere in the signature of your method )
    Last edited by GanglyLamb; 02-01-2006 at 10:54 AM.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    Many thanks for that - my brain hurts and that really helped.

    Thanks

  4. #4
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    If your brain really hurts I suggest seeing a doctor, else take a rest and get back to work when your mind is crystalclear .

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    38

    Complete method for anyone interested

    Code:
    private static int RoundToNearest100(int iNumberToRound)
    		{
    			int iToNearest = 100;
    			int iNearest = 0;
    			bool bIsUpper = false;
    
    			int iRest = iNumberToRound % iToNearest;
    			if (iNumberToRound == 550) bIsUpper = true;
    
    			if (bIsUpper == true)
    			{
    				iNearest = (iNumberToRound - iRest) + iToNearest;
    				return iNearest;
    			}
    			else if (iRest > (iToNearest/2))
    			{
    				iNearest = (iNumberToRound - iRest) + iToNearest;
    				return iNearest;
    			}
    			else if (iRest < (iToNearest/2))
    			{
    				iNearest =(iNumberToRound - iRest);
    				return iNearest;
    			}
    
    			return 0;
    		}

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    cant rest - have a deadline for 7th Feb!!!! ahhhh.....

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Alternately, you might try:
    Code:
    static int Round(int number, int place)
    {
    	if (place == 0)
    		return number;
    
    	if (number % (int)Math.Pow(10, place) >= 5 * (int)Math.Pow(10, place-1))
    		return (number / (int)Math.Pow(10, place) + 1) * (int)Math.Pow(10, place);
    	return (number / (int)Math.Pow(10, place)) * (int)Math.Pow(10, place);
    }
    I was kind of surprised that the Math class doesn't already have a static method to round an integer.
    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

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,261
    Alternately, you might try:
    I think the code he has already is a far better solution.

  9. #9
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by bithub
    I think the code he has already is a far better solution.
    Depends on how you define 'better'. One function does one specific task. The other is capable of doing many tasks. Further, the function I wrote will never return an obviously wrong answer.
    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

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,261
    Depends on how you define 'better'
    I define better as running over 15 times as fast. I'm not exaggerating at all, I actually timed the two functions.

    Here is a far better implementation of your function:
    Code:
        static int RoundFast(int number, int place)
        {
            int i = 1;
            if (place <= 0)
                return number;
            while (place > 0)
            {
                i = i * 10;
                place--;
            }
    
            int r = number % i;
            if (r < (i / 2))
                return number - r;
            else
                return number - r + i;
        }
    This runs at the same speed as the RoundToNearest100 function the OP came up with (about 15 times as fast as your implementation).

  11. #11
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    I'd agree that your function is better than either one. I'm not surprised that mine was slow; using Math.Pow is just asking for a speed hit.
    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

  12. #12
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Well this is how I round to the nearest whole number:

    Code:
    float someNumber = 798.5600F;
    int closestToOneThousand = (int)(someNumber + 0.5);
    Adding 0.5 turns it to 799.06. Casting this to an integer rounds it down to the nearest int, so thats 799.

    If the number was say 798.46, adding 0.5 would have turned it to 798.96, and when cast to an int thats 798.

    We can then take that further:
    Code:
    int roundTo = 1000;
    float someNumber = 798.5600F;
    int closestToOneThousand = (int)((someNumber + (0.5 * roundTo)) / roundTo) * roundTo;
    This adds 0.5*1000 (500) to the number, making it 1298.56. Then, we divide by 1000, making it 1.298.56. Casting that to an int gives us 1, which we multiply by 1000 (cancelling our devision earlier) to leave 1000, which is the correct number.

    Edit: And, I think since mine just uses casting and math, it's going to be faster than yours.

    Warning: This only works where casting to an integer always returns just the whole number and ignores the decimal part. C, C++, Java and C# all behave this way I believe, but some other languages might not.

  13. #13
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    Stovellp to the rescue!

    You are absolutely right, calling the method etc will probably be slower then just doing some calculations and an explicit cast.

    Using an approach like that never even crossed my mind when replying to the OP.

  14. #14
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,261
    Edit: And, I think since mine just uses casting and math, it's going to be faster than yours.
    Actually my version was still about 5-10% faster. Yours is a bit slower than you would think due to the fact that it has to use the FPU which is significantly slower than the CPU.

  15. #15
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Quote Originally Posted by bithub
    Actually my version was still about 5-10% faster. Yours is a bit slower than you would think due to the fact that it has to use the FPU which is significantly slower than the CPU.
    I didn't realise he only wanted to round integers. He could just use an integer with mine as well in this case (since he's multiplying by 500). Then he might not even need to do any casting. That's something to experiment with

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. is it ok like that?
    By ExDHaos in forum C++ Programming
    Replies: 8
    Last Post: 05-23-2009, 10:02 AM
  2. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 09:55 AM
  3. Simulator
    By MasterAchilles in forum C Programming
    Replies: 10
    Last Post: 11-30-2008, 10:31 PM
  4. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  5. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 12:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21