3/24/2011 12:36:50 AM
Title:
Finding Closer Number In ArrayCollection
Hello,
My Task is, I having number say 7 now I am having array collection say(5,10,15,20), Now I provide this array collection to combobox,. so when there is number 7 I want to find closest number from array collection and and bring combobox.selectedItem to that closest number( here 5)
So is there any function or API which helps me in this???
Please Help ..!
3/26/2011 11:52:24 AM
Use the below function in actionscript 3 to find the nearest or closest number in an array. You need to pass the array and the number as a parameter to the below function
//to call the function pass the array and the number to search
nearestNumber(myArray,7);
//use this function to find the closest or nearest number in the array
private function nearestNumber(myArr:Array, num:Number):Number {
var nearNum:Number;
var oldDifference:Number;
for (var x:int=0; x<myArr.length; x++) {
var newDifference:Number = Math.abs(Number(myArr[x]) - num);
if (newDifference <= oldDifference) {
oldDifference = newDifference;
nearNum = myArr[x];
}
}
return nearNum;
}
3/28/2011 2:31:38 AM
thanks that worked pretty well for me