As I was developing an application, I needed to convert degrees celcius to fahrenheit and vice versa,
as well as mph to kph and vice versa.
I figured the best way to implement this was in a util class because these calculations aren’t likely to change any time soon
After some googling for a class like this, i didn’t find something alike.
So that’s why I decided to write my own and of course share it with you. I tried to put in the most commonly used conversions.
This class enables you to convert:
- miles to kilometers, kilometers to miles
- celsius to fahrenheit, fahrenheit to celsius
- meters to feet, feet to meters
- … and many more
This is the result:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | /** * ... * @author Dennis Jaamann * @version 1.0 * Util Class that converts units from one given unit to another * * Copyright (c) 2009 Dennis Jaamann Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package com.dj.util { public class UnitConverter { /** * Length */ public static function kilometersToMiles(kilometers:Number):Number{ return kilometers * 0.621371192; } public static function kilometersToNauticalMiles(kilometers:Number):Number{ return kilometers * 0.539956803; } public static function milesToKilometers(miles:Number):Number{ return miles * 1.609344; } public static function milesToNauticalMiles(miles:Number):Number{ return miles * 0.868976242; } public static function nauticalMilesToKilometers(nauticalMiles:Number):Number{ return nauticalMiles * 1.85200; } public static function nauticalMilesToMiles(nauticalMiles:Number):Number{ return nauticalMiles * 1.15077945; } public static function metersToFeet(meters:Number):Number{ return meters * 3.2808399; } public static function feetToMeters(feet:Number):Number{ return feet * 0.3048; } public static function metersToYards(meters:Number):Number{ return meters * 1.0936133; } public static function yardsToMeters(yards:Number):Number{ return yards * 0.9144; } public static function centimetersToInches(centimeters:Number):Number{ return centimeters * 0.393700787; } public static function inchesToCentimeters(inches:Number):Number{ return inches * 2.54; } /** * Temperature */ public static function fahrenheitToCelsius(degrees:Number):Number{ return (degrees - 32) / 1.8; } public static function fahrenheitToKelvin(degrees:Number):Number{ return (degrees + 459.67) / 1.8; } public static function celsiusToFahrenheit(degrees:Number):Number{ return (degrees * 1.8) + 32; } public static function celsiusToKelvin(degrees:Number):Number{ return degrees + 273.15; } public static function kelvinToCelsius(degrees:Number):Number{ return degrees - 273.15; } public static function kelvinToFahrenheit(degrees:Number):Number{ return (degrees * 1.8) - 459.67; } /** * Weight */ public static function kilogramsToPounds(kilograms:Number):Number{ return kilograms * 2.20462262; } public static function kilogramsToStone(kilograms:Number):Number{ return kilograms * 0.157473044; } public static function poundsToKilograms(pounds:Number):Number{ return pounds * 0.45359237; } public static function poundsToStone(pounds:Number):Number{ return pounds * 0.0714285714; } public static function stoneToPounds(stone:Number):Number{ return stone * 14; } public static function stoneToKilograms(stone:Number):Number{ return stone * 6.35029318; } public static function gramsToOunces(grams:Number):Number{ return grams * 0.0352739619; } public static function ouncesToGrams(ounces:Number):Number{ return ounces * 28.3495231; } /** * Speed */ public static function kilometersPerHourToKnots(kilometersPerHour:Number):Number{ return kilometersPerHour * 0.539956803; } public static function kilometersPerHourToMilesPerHour(kilometersPerHour:Number):Number{ return kilometersPerHour * 0.621371192; } public static function milesPerHourToKnots(milesPerHour:Number):Number{ return milesPerHour * 0.868976242; } public static function milesPerHourToKilometersPerHour(milesPerHour:Number):Number{ return milesPerHour * 1.609344; } public static function knotsToKilometersPerHour(knots:Number):Number{ return knots * 1.85200; } public static function knotsToMilesPerHour(knots:Number):Number{ return knots * 1.15077945; } /** * Volume */ public static function litersToGallons(liters:Number):Number{ return liters * 0.264172052; } public static function gallonsToLiters(gallons:Number):Number{ return gallons * 3.78541178; } public static function cubicFeetToCubicMeters(cubicFeet:Number):Number{ return cubicFeet * 0.0283168466; } public static function cubicMetersToCubicFeet(cubicMeters:Number):Number{ return cubicMeters * 35.3146667; } /** * Power */ public static function kilowattsToHorsepower(kilowatts:Number):Number{ return kilowatts * 1.34102209; } public static function horsepowerToKilowatts(horsepower:Number):Number{ return horsepower * 0.745699872; } } } |
Usage:
1 2 3 4 5 6 7 8 9 10 11 12 | package { public class SomeClass { public function SomeClass() { var kph:Number = UnitConverter.milesPerHourToKilometersPerHour(10); trace("10 miles per hour = " + kph + " kilometers per hour"); } } } |
Copy text or download source: unitconverter (534)

#1 by Jupi on April 16th, 2009 - 10:05 am
Hi,
I would like to develop a utility class too, and first time I did it the same way as yours.
But I noticed the increase of the size (in kilobytes) of the swf that used my util class.
And also noticed that (eg. flash.utils.getTimer()) that functions can be placed inside a package without a class, so those functions won’t be static. In the source flash/utils/package.as contains the declaration/interface of the functions but not the definitions of it.
Ther I created a package.as in my package structure, and placed my utility functions’ definition there. It did not worked. I wonder why. Adobe can do it and I cannot? XD
Any idea?
Thanks!
Sincerely,
Jupi
#2 by Dennis on April 16th, 2009 - 11:28 am
@Jupi
I have taken a look at the behaviour you pointed out and the reason why adobe does this kind of declaration is also unknown to me.
My guess is that the structure set up by adobe is there for a reason (namespaces, intrinsic / dynamic classes).
It would be logical to assume that they made the core in a way that is open for extension but closed for modification.
The fact that when you compile your project with you own util class, size is added seems rather logical to me. When it is imported once, the file is added to your compile.
A normal util class should normally not even be larger than say.. 15k?
I do realize that the problem you are adressing can be of great importance when dealing with bandwith restrictions. But in other cases 15k added to the total size of a project seems rather trivial to me.
Check out some util classes by grant skinner and senocular, both very respected members in the flash community. Then you will find they kinda do it in the same way as above.
Maybe you could drop your question to them, because they have far more understanding and experience in this matter.
Regards,
Dennis
#3 by Figueras on December 21st, 2009 - 9:42 pm
Hello can i have some help to use this action in FLA ?
Because this source is great but i don’t know how to use it in a FLA
Thank you !
#4 by DeanLogic on March 12th, 2010 - 7:24 pm
Nice set of conversions. Thanks!
I’m making a Kitchen Calculator, so these will help with some of the conversions. I’ll just add the others along side of yours.
BTW, you might want to look into the SyntaxHighlighter Evolved plugin for WordPress. It has better tools for copying the code to the clipboard.