Results 1 to 2 of 2
  1. #1
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Help with finding nearest multiple of 250

    I have an assignment to take the number of pints of popcorn a farm produces and divide it by the number of acres on the farm. After that, I need to
    figure out the nearest factor of that number and 250. For example, if I have 567 pints/acre, I would need to round it down to a multiple of 250, so this
    would return a value of 2, and so on. I've been using the % operand, but I can't quite figure out how to round it down to the proper multiple.
    Here's a bit of my code to show what I mean.
    Java Code:
    		pintAcre = pints / acres;
      			while(pintAcre%x == 250){
      				x++;
      				pintFactor[x] = pintAcre;
      				
      			}
    X would continually increase in the loop to find the product of the math. But unfortunately, it only returns 0. I'm not very experienced with %, and I'm not sure how to go about it. Thanks for any help.

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default Re: Help with finding nearest multiple of 250

    Its been a while since i did something like this but give it a go :/

    You could use mod (%) and deal with the remainder: This probably isn't the most efficient way but it works for me

    This is an example of dealing with remainders, i used different variables just to show you, you will need to adapt this to suit you

    Java Code:
                    int pintAcre = 500;
    		int testRU = 375;
    		int testRD = 11123;
    
    		if((pintAcre % 250) == 0){
    			System.out.println("Exactly multiple of 250: " + pintAcre);
                          
    }
    				if((testRD % 250) <= 124){
    			int x = testRD - (testRD % 250); //if remainder is below half way then just remove the remainder from the value, which will become a rounded down multiple of 250
    			System.out.println("rounded down: " + x);
    }
    				if((testRU % 250) >= 125){
    			int y = testRU - (testRU % 250) + 250 ; // if remainder is above or equal to half way, then take the number subtract the remainder from it so you get to the rounded down multiple of 250, then add 250 to it, which rounds it nearest 250
    			System.out.println("Rounded up : " + y);
    }
    Prints:

    Exactly multiple of 250: 500
    rounded down: 11000
    Rounded up : 500


    Example of the round up method:

    375 % 250 = 125.

    125 is half way do round up:

    375 - (375 % 250) = 250

    250 + 250 = 500.

    375 to nearest 250 = 500
    Last edited by monkeyjr97; 05-11-2013 at 11:26 PM.

Similar Threads

  1. Replies: 2
    Last Post: 05-03-2013, 03:55 AM
  2. How do I round a number to nearest 2.5 multiple?
    By LasseA in forum New To Java
    Replies: 2
    Last Post: 11-25-2012, 01:11 PM
  3. One process open multiple threads on multiple CPUs
    By kfcnhl in forum Threads and Synchronization
    Replies: 2
    Last Post: 12-12-2011, 09:27 AM
  4. multiple Data from Jsp that having same name of multiple input tage to servlet
    By rahul9323.2007@gmail.com in forum Advanced Java
    Replies: 0
    Last Post: 07-29-2011, 02:00 PM
  5. Need nearest neighbor code
    By reddish in forum New To Java
    Replies: 4
    Last Post: 09-20-2008, 03:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •