Results 1 to 2 of 2
 05112013, 09:34 PM #1Member
 Join Date
 Nov 2012
 Posts
 40
 Rep Power
 0
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; }
 05112013, 09:57 PM #2Senior Member
 Join Date
 Nov 2012
 Posts
 257
 Rep Power
 5
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); }
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 = 500Last edited by monkeyjr97; 05112013 at 10:26 PM.
Similar Threads

Help finding a substring in a text file with multiple lines and printing all of them.
By herpeslurpy in forum New To JavaReplies: 2Last Post: 05032013, 02:55 AM 
How do I round a number to nearest 2.5 multiple?
By LasseA in forum New To JavaReplies: 2Last Post: 11252012, 01:11 PM 
One process open multiple threads on multiple CPUs
By kfcnhl in forum Threads and SynchronizationReplies: 2Last Post: 12122011, 09:27 AM 
multiple Data from Jsp that having same name of multiple input tage to servlet
By rahul9323.2007@gmail.com in forum Advanced JavaReplies: 0Last Post: 07292011, 01:00 PM 
Need nearest neighbor code
By reddish in forum New To JavaReplies: 4Last Post: 09202008, 02:38 PM
Bookmarks