Solved

# Return the closest number in a list to a given value

Posted on 2006-04-06

Hi there.

The question is simple really.

I have a list which adds a integers in a loop to it based on the outcome of certain conditions. At the end of that loop, I want to find out which is the closest number in that list to a value I already have.

How would I do that?

Example:

int myInt = 13;

myList (after going through the loop) = {1,16,20,31,2,12,4}

getClosestNumber(myInt, myList)

public int getClosestNumber(int value, List valueList)

{

run marvellous check I don't know how to do.

return int; //In this case 12. If there are two numbers (ie, if 14 is also in the list, return the lowest number).

}

Thanks in advance