Units API
raspberryman - January 5, 2009 - 21:07
Units API converts between various weights and measurements.
The central API call, unitsapi_convert($value, $from, $to, $details = FALSE), uses the International System of Units (SI) conversion factors to convert measurement units.
This module is only an API and does not have an user interface.
SUPPORTED UNITS
A list of supported units, and the keys you need to pass to the unitsapi_convert() function, are available on the module's help page.
EXAMPLES
<?php
// Convert kilometer to feet
$result = unitsapi_convert(1.5, 'kilometer', 'foot');
// $result == 4921.259843
// Convert Fahrenheit to Kelvin
$result = unitsapi_convert(55, 'fahrenheit', 'kelvin');
// $result == 285.927778
// Convert US liquid ounces to Imperial pints with a detailed array of the conversion
$result = unitsapi_convert(50, 'us fluid ounce', 'imperial pint', TRUE);
// $result == Array ([result] => 2.602107, [from] => US fluid ounces, [to] => Imperial pints)
?>HOOK_UNITSAPI_ALTER()
Use hook_unitsapi_alter(&$units) to make changes to the units array. For example:
<?php
/**
* Implementation of hook_unitsapi_alter().
*/
function mymodule_unitsapi_alter(&$units) {
$units['meter']['singular'] = 'metre';
}
?>HOW TO ADD NEW UNITS
- Option #1: Implement hook_unitsapi_alter() from your own module to add units.
- Option #2: Create an issue with your requested units. The issue requires at least the unit name, plural name, symbol, conversion factor, and an URL to a credible source to verify the conversion factor. For the conversion factor, check the units.xml file for the common factor. For example, all Area conversions use square meters as their conversion factor.
INTEGRATED MODULES
FUTURE PLANS
- Add additional measurement units.
- Add additional automated tests.
SPONSORED BY
This module is sponsored by Raspberry Man LLC.
