Posted By

allnatural on 05/28/10


Tagged

percentage


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

socialflasher


Return a % that a number is between 2 other numbers


 / Published in: ActionScript 3
 

  1. /**
  2. * Returns a percentage of a value in between 2 other numbers.
  3. * @param bottomRange low end of the range.
  4. * @param topRange top end of the range.
  5. * @param valueInRange value to find a range percentage of.
  6. * @return The percentage of valueInRange in the range.
  7. * @use getPercentWithinRange( 50, 150, 100 ); // displays 50
  8. */
  9. public static function getPercentWithinRange( bottomRange:Number, topRange:Number, valueInRange:Number ):Number
  10. {
  11. // normalize values to work off zero
  12. if (bottomRange < 0)
  13. {
  14. var addToAll:Number = Math.abs(bottomRange);
  15. bottomRange += addToAll;
  16. topRange += addToAll;
  17. valueInRange += addToAll;
  18. }
  19. else if ( bottomRange > 0 )
  20. {
  21. var subFromAll:Number = Math.abs(bottomRange);
  22. bottomRange -= subFromAll;
  23. topRange -= subFromAll;
  24. valueInRange -= subFromAll;
  25. }
  26. // simple calc to get percentage
  27. return 100 * ( valueInRange / ( topRange - bottomRange ) );
  28. }

Report this snippet  

You need to login to post a comment.