Return to Snippet

Revision: 27209
at May 28, 2010 17:45 by allnatural


Initial Code
/**
		 * Returns a percentage of a value in between 2 other numbers.
		 * @param bottomRange  low end of the range.
		 * @param  topRange  top end of the range.
		 * @param  valueInRange value to find a range percentage of.
		 * @return The percentage of valueInRange in the range.
		 *  @use getPercentWithinRange( 50, 150, 100 );  // displays 50
		 */
		public static function getPercentWithinRange( bottomRange:Number, topRange:Number, valueInRange:Number ):Number
		{
			// normalize values to work off zero
			if (bottomRange < 0)
			{
				var addToAll:Number = Math.abs(bottomRange);
				bottomRange +=  addToAll;
				topRange +=  addToAll;
				valueInRange +=  addToAll;
			}
			else if ( bottomRange > 0 )
			{
				var subFromAll:Number = Math.abs(bottomRange);
				bottomRange -=  subFromAll;
				topRange -=  subFromAll;
				valueInRange -=  subFromAll;
			}
			// simple calc to get percentage 
			return 100 * ( valueInRange / ( topRange - bottomRange ) );
		}

Initial URL

                                

Initial Description

                                

Initial Title
Return a % that a number is between 2 other numbers

Initial Tags

                                

Initial Language
ActionScript 3