/ Published in: ActionScript 3
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * 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 ) ); }