Previous Page
Next Page

Recipe 4.15. Converting Between Units of Measurement

Problem

You want to convert between Fahrenheit and Celsius, pounds and kilograms, or other units of measure.

Solution

Use the Unit and Converter classes.

Discussion

There are various systems of measurement used throughout the world. For example, temperature is commonly measured in both Fahrenheit and Celsius. Weight is sometimes measured in pounds, but quite frequently, a metric system that uses kilograms is employed instead. And distances are likely to be measured in miles instead of kilometers, or inches instead of centimeters. For these reasons, you may need to convert from one unit of measure to another.

Technically, pounds are a measurement of weight, while kilograms are a measurement of mass. Weight is a force that changes as the acceleration due to gravitational changes, whereas mass is constant regardless of the effects of gravity. The effect is that an object's weight on the moon and Earth differ since there are different effects, thanks to gravity, but the mass of an object remains the same. However, on the surface of the Earth, mass and weight often are used interchangeably.


Each of these conversions has its own algorithm. For example, to convert from Centigrade to Fahrenheit, you should multiply by 9, divide by 5, and then add 32 (to convert from Fahrenheit to Centigrade, subtract 32, multiply by 5, and then divide by 9). Likewise, you can multiply by 2.2 to convert pounds to kilograms, and you can divide by 2.2 to convert kilograms to pounds. (We also saw in Recipe 4.12 how to convert angles from degrees to radians and vice versa.)

As with converting between different units of measure with angles, you can use the Unit and Converter classes to convert between other types of units. The Unit class has support for quite a range of categories of measurement (angles, temperature, volume, etc.). You can retrieve an array of supported categories using the static Unit.getCategories( ) method:

// Display the categories that are supported.
trace(Unit.getCategories(  ));

For each category, there are a variety units are supported. For example, in the angle category degrees, radians, and gradians are supported. You can retrieve a list of supported units for a given category using the static Unit.getUnits( ) method. Pass the method a category name to retrieve an array of units supported for that group. If you omit the parameter, then the entire list of supported units is returned:

// Display the units supported in the temperature category.
trace(Unit.getUnits("temperature"));

You can use the built-in Unit constants for any supported unit of measurement, just as in Recipe 4.12. Then you can retrieve a Converter object by using the getConverterTo( ) or getConverterFrom( ) methods. The following example creates a Converter to calculate the Fahrenheit equivalents of Celcius measurements:

var converter:Converter = Unit.CELCIUS.getConverterTo(Unit.FAHRENHEIT);

Then, of course, you can use the convert( ) (and/or the convertWithLabel( )) method to convert values:

trace(converter.convert(0));  // Displays: 32

See Also

For an example of using a hardcoded function to perform a single type of unit conversion, refer to Recipe 4.12, which includes a function to convert angles from degrees to radians and another function that does the opposite.


Previous Page
Next Page
Converted from CHM to HTML with chm2web Pro 2.85 (unicode)