Convert values within a range to values within another range

Today I’m sharing with you a little utility that I find myself using a lot lately. This utility is a value convertor that you can use in those times when you need to make a volume slider or something similar where you’ll need to change a value and a value range.

package com.vamapaull.utils
{	
	public class ValueConvertor
	{
		public static function convertRange(originalStart:Number, 
							originalEnd:Number, 
							newStart:Number, 
							newEnd:Number, 
							value:Number):Number
		{
			var originalRange:Number = originalEnd - originalStart;
			var newRange:Number = newEnd - newStart;
			var ratio:Number = newRange / originalRange;
			var newValue:Number = value * ratio;
			var finalValue:Number = newValue + newStart;
			return finalValue;
		}
	}
}
Timecode Utility

Today I’m sharing simple utility that I use from time to time when I need to convert time (see what I did there? 😀 )
It’s very useful if you build a FLV player for example, and want to convert the time into minutes:seconds

Example of how to apply it to your project:

import com.vamapaull.utils.TimeUtil;
 
time.text = TimeUtil.getTimecode(timeValue);

Results:

The ActionScript class:

package com.vamapaull.utils
{
    public class TimeUtil
    {
        public static function getTimecode(value:Number):String
        {
            var t:Number    = Math.round(value),
                min:Number  = Math.floor(t/60),
                sec:Number  = t%60,
                tc:String   = "";
 
            if(min < 10)                  tc += "0";                          if(min >= 1)
            {
                if (isNaN(min) == true) tc += "0";
                else tc += min.toString();
            }
            else 
                tc += "0";
 
            tc += ":";
 
            if(sec < 10) 
            {
                tc += "0";
 
                if (isNaN(sec) == true) tc += "0";
                else tc += sec.toString();
            }
            else 
            {
                if (isNaN(sec) == true) tc += "0";
                else tc += sec.toString();
            }
 
            return tc;
        }
    }
}