Download demo project - 18 Kb
Download source files - 7 Kb
<!-- Article Starts -->

This library offers functions to convert values between units of measurement.
It's very simple to use. For example, to convert between **mph** (miles per hour)
to **m/s** (meters per second), just use the following code:

double dValue = 1.0;
TcxConvertUnit( dValue, _T("mph"), _T("m/s") );

The units are represented as strings, as the intention of the library is to afford unit
entrance support at the UI level. In fact, the Demo Application that accompanies this
article is a very simple (yet powerful) unit conversion utility.

The table of supported units is currently hard coded. You can add your own new units
just following the instructions in the source file (**TcxUnitConverter.cpp**).

The unit interpreter accepts all the multiplication prefixes of the
**International System of Units** (see the Appendices section).

The unit interpreter accepts also linear unit expressions. For example, you can convert
from **acre** (Acres) to **mi^2** (square miles), or from **N** (Newtons,
the unit of strength in the **International System of Units**) to **m*kg/s^2**
(meter times kilogram per square second).

The following are samples of valid unit expressions (they are all units of strength).

**m*kg/s^2**
**m*kg*s^-2**
**m/kg^-1*s*s**
**kg*s^-2/m^-1**
There can be only one division ('/') symbol in the expression, and all right terms are
considered as part of the denominator (e.g. **m/s^2** equals **m/s*s**). Parenthesis
are not used. Exponents, when applicable, must be positive or negative literal integers.

**This source code is provided "as is" without express or implied warranty.**
Use it at your own risk! While I have made every effort to remove any undesirable
"bugs", I cannot be held responsible if it causes any damage or loss of time, data, or Martian
probes. |

### Known Limitations

The unit converter can only perform the following type of conversion:

Unit1 = K * Unit2;

This is valid for the greatest multitude of the common day units we can think of,
but for temperature units.

Temperature units as **Kelvin**, **Celsius** and **Fahrenheits** all demand
conversion formulas in the following form:

Unit1 = K * Unit2 + K0;

The library will still convert between these units, but, in this case, the value is
supposed to be a temperature variation rather than an absolute temperature.

### Things to do

Implement UI Controls that automatically make use of these features.
Implement a more flexible Unit Converter so sets of units can be "loaded" and
combined in runtime.
Buy a Ferrari.

### Using the Demo Application

In the **From/Value** field, enter the source value you want to convert.
In the **From/Unit** field, type the unit of the source value, or choose one
from the dropdown list. In the **To/Unit** field, type the target unit
or choose one from the dropdown list. The result will be displayed in the
**To/Result** just-in-time. You can select & copy from this field.

To enter unit expression in either the **From/Unit** or the **To/Unit** fields,
you must type them. The dropdown lists show only sigle units.

### Appendices

**Table 1:** Multiplication prefixes of the **International System of Units**

Prefix | Name | Constant |

Y | Yotta | 1e+24 |

Z | Zetta | 1e+21 |

E | Exa | 1e+18 |

P | Peta | 1e+15 |

T | Tera | 1e+12 |

G | Giga | 1e+9 |

M | Mega | 1e+6 |

k or K | kilo | 1e+3 |

h or H | hecto | 1e+2 |

D | Deka | 1e+1 |

d | deci | 1e-1 |

c | cent | 1e-2 |

m | mili | 1e-3 |

u or µ | micro | 1e-6 |

n | nano | 1e-9 |

p | pico | 1e-12 |

f | femto | 1e-15 |

a | atto | 1e-18 |

z | zepto | 1e-21 |

y | yocto | 1e-24 |

**Table 2:** Basic Units

Abbr | Tag |

A | Ampere |

K | Kelvin |

cd | Candela |

g | Gram |

m | Meter |

mol | Mole |

r | Radian |

s | Second |

sr | Steradian |

**Table 3:** Derived Units

Abbr | Tag | Definition |

Bq | becquerel | 1 [l/s] |

Btu | InternationalTableBtu | 1055.05585262 [Kg*m^2/s^2] |

C | Coulomb | 1 [A*s] |

Ci | Curie | 3.7*1010 [l/s] |

F | Farad | 1 [A^2*s^4/Kg*m^2] |

Fdy | Faraday | 96487 [A*s] |

Gy | Gray | 1 [m^2/s^2] |

H | Henry | 1 [Kg*m^2/A^2*s^2] |

Hz | Hertz | 1 [s^-1] |

J | Joule | 1 [Kg*m^2/s^2] |

Knot | NauticalMilesPerHour | 463.0/900.0 [m/s] |

N | Newton | 1 [Kg*m/s^2] |

Ohm | Ohm | 1 [Kg*m^2/A^2*s^3] |

OzUK | UKFluidOunce | 2.8413075e-5 [m^3] |

P | Poise | 0.1 [Kg/m*s] |

Pa | Pascal | 1 [Kg/m*s] |

Pdl | Poundal | 0.13825495376 [Kg*m/s^2] |

Pica | Pica | 1.0/72.0 [in] |

R | Roentgen | 0.000258 [A*s/Kg] |

S | Siemens | 1 [A^2*s^3/Kg*m^2] |

St | Stokes | 0.0001 [m^2/s] |

Sv | Sievert | 1 [m^2/s^2] |

T | Tesla | 1 [Kg/A*s^2] |

U | UnifiedAtomicMass | 1.6605402e-27 [Kg] |

V | Volt | 1 [Kg*m^2/A*s^2] |

W | Watt | 1 [Kg*m^2/s^3] |

Wb | Weber | 1 [Kg*m^2/A*s^2] |

acre | Acre | 4046.87260987 [m^2] |

arcmin | ArcMinute | 2.9088820866e-4 [r] |

arcs | ArcSecond | 4.848136111e-6 [r] |

atm | Attmosphere | 101325 [Kg/m*s^2] |

au | AstronomicalUnit | 1.495979*1011 [m] |

b | Barn | 1e-28 [m^2] |

bar | Bar | 100000 [Kg/m*s^2] |

bbl | Barrel | 0.158987294928 [m^3] |

bu | Bushel | 0.03523907 [m^3] |

c | LightSpeed | 299792458 [m/s] |

cal | Calorie | 4.1868 [Kg*m^2/s^2] |

chain | Chain | 20.1168402337 [m] |

ct | Carat | 0.0002 [Kg] |

cu | USCup | 2.365882365e-4 [m^3] |

d | day | 86400 [s] |

dyn | Dyne | 0.00001 [Kg*m/s^2] |

eV | ElectronVolt | 1.60217733e-19 [Kg*m^2/s^2] |

erg | Erg | 0.0000001 [Kg*m^2/s^2] |

fath | Fathom | 1.82880365761 [m] |

fbm | BoardFoot | 0.002359737216 [m^3] |

fc | Footcandle | 10.7639104167 [cd*sr/m^2] |

fermi | Fermi | 1e-15 [m] |

flam | Footlambert | 3.42625909964 [cd/m^2] |

ft | InternationalFoot | 0.3048 [m] |

ftUS | SurveyFoot | 0.304800609601 [m] |

ga | StandardFreefall | 9.80665 [m/s^2] |

gal | USGallon | 0.003785411784 [m^3] |

galC | CanadianGallon | 0.00454609 [m^3] |

galUK | UKGallon | 0.004546092 [m^3] |

gf | GramForce | 0.00980665 [Kg*m/s^2] |

grad | Grade | 1.57079632679e-2 [r] |

grain | Grain | 0.00006479891 [Kg] |

h | Hour | 3600 [s] |

ha | Hectare | 10000 [m^2] |

hp | horsepower | 745.699871582 [Kg*m^2/s^2] |

in | Inch | 0.0254 [m] |

inH2O | InchesOfWater | 248.84 [Kg/m*s^2] |

inHg | InchesOfMercury | 3386.38815789 [Kg/m*s^2] |

kip | KilopoundForce | 4448.22161526 [Kg*m/s^2] |

kph | KilometersPerHour | 5.0/18.0 [m/s] |

l | Liter | 0.001 [m^3] |

lam | Lambert | 3183.09886184 [cd/m^2] |

lb | AvoirdupoisPound | 0.45359267 [Kg] |

lbf | PoundForce | 4.44822161526 [Kg*m/s^2] |

lbt | TroyPound | 0.3732417216 [Kg] |

lm | Lumen | 1 [cd*sr] |

lx | Lux | 1 [cd*sr/m^2] |

lyr | LightYear | 9.46052840488*1015 [m] |

mho | Mho | 1 [A^2*s^2/Kg*m^2] |

mi | InternationalMile | 1609.344 [m] |

miUS | USStatuteMile | 1609.34721869 [m] |

mil | Mil | 0.0000254 [m] |

min | Minute | 60 [s] |

mmHg | MilimeterOfMercury | 133.322368421 [Kg/m*s^2] |

mph | MilesPerHour | 0.44704 [m/s] |

nmi | NauticalMile | 1852 [m] |

oz | Ounce | 0.028349523125 [Kg] |

ozfl | USFluidOunce | 2.95735295625e-5 [m^3] |

ozt | TroyOunce | 0.0311034768 [Kg] |

pc | Parsec | 3.08567818585106 [m] |

ph | Phot | 10000 [cd*sr/m^2] |

pk | Peck | 0.0088097675 [m^3] |

psi | PoundsPerSquareInch | 6894.75729317 [Kg/m*s^2] |

pt | Pint | 0.000473176473 [m^3] |

qt | Quart | 0.000946352946 [m^3] |

rad | Rad | 0.01 [m^2/s^2] |

rd | Rod | 5.02921005842 [m] |

rem | Rem | 0.01 [m^2/s^2] |

sb | Stilb | 10000 [cd/m^2] |

slug | Slug | 14.5939029372 [Kg] |

st | Stere | 1 [m^3] |

t | MetricTon | 1000 [Kg] |

tbsp | Tablespoon | 1.47867647813e-5 [m^3] |

therm | EECTherm | 105506000 [Kg*m^2/s^2] |

ton | ShortTon | 907.18474 [Kg] |

tonUK | UKLongTon | 1016.0469088 [Kg] |

torr | Torr | 133.322368421 [Kg/m^2] |

tsp | Teaspoon | 4.92892159375e-6 [m^3] |

yd | InternationalYard | 0.9144 [m] |

yr | Year | 31556925.9747 [s] |

° | Degree | 1.74532925199e-2 [r] |

°C | DegreeCelsius | 1.0 [K] |

°F | DegreesFahrenheit | 1.0/1.8 [K] |

°R | DegreesRankine | 1.0/1.8 [K] |

µ | Micron | 1e-6 [m] |

Å | Angstrom | 1e-10 [m] |