GNU Units – for conversion between units

Again Planet Ubuntu has introduced me to a new tool, may be not new for you, which is very useful in day to day life. Its `units`, which helps in the conversion of one unit to another. It can be invoked from the command line (aka terminal), either by passing the input and output units as arguments or using the interactive mode. It supports 2438 units, 71 prefixes and 32 nonlinear units.

When you just call the units, it fires up the interactive mode,

$ units
2438 units, 71 prefixes, 32 nonlinear units

You have: _

Now we have to enter the input unit, that is the unit we want to be converted. Say “1 inch” to be converted to cms. For this “You have:” is “1 inch” and “You want:” is “cm”.

You have: 1 inch
You want: cm
* 2.54
/ 0.39370079

It gives two outputs marked by * and /. The first one (marked by *) is the forward conversion, i.e., inches are represented in cms. The second one (marked by /) is the reverse conversion, i.e., it is the representation of 1 cm in 1 inch.

Let us look at some common conversions which we might me interested in general.

You have: inch
You want: cm
* 2.54
/ 0.39370079
You have: feet
You want: cm
* 30.48
/ 0.032808399
You have: feet
You want: inch
* 12
/ 0.083333333
You have: meter
You want: inch
* 39.370079
/ 0.0254
You have: meter
You want: cm
* 100
/ 0.01
You have: kilometer
You want: inch
* 39370.079
/ 2.54e-05

We have looked at conversion of units for length|height. Now lets see for weight and volume.

You have: kg
You want: lb
* 2.2046226
/ 0.45359237
You have: liter
You want: m^3
* 0.001
/ 1000
You have: gallon
You want: liter
* 3.7854118
/ 0.26417205
You have: gallon
You want: m^3
* 0.0037854118
/ 264.17205

There is one more common conversion left out, degC to degF and vice versa.

You have: degC
You want: degF
* 1.8
/ 0.55555556

These are basic conversions where we compared one unit of input to corresponding output. Now lets looks at different values and the corresponding results. There is an interesting thing to note here.

You have: 5 inch
You want: cm
* 12.7
/ 0.078740157

A inch is 2.54 cm and 0.39370079 cm is one inch. Looking at the current conversion, 5 inches is 5 *2.54 cms= 12.7 cms. But what is the second value “0.078740157” ? This is nothing but the 1 cm with respect to 5 inches, or how much 1 cm is a part of 5 inches (1/(2.54*5) = 1/12.7 = 0.078740157). It might be a bit confusing at the beginning, but this is useful at times when we want to know the reverse ratio as well.

You have: 8 meter
You want: inch
* 314.96063
/ 0.003175
You have: 400 feet
You want: cm
* 12192
/ 8.2020997e-05
You have: 72 kg
You want: lb
* 158.73283
/ 0.006299894

Now, let us try to convert between units which do not have much relation.

You have: kg
You want: liter
conformability error
1 kg
0.001 m^3
You have: kg
You want: m^3
conformability error
1 kg
1 m^3

Though it shows the conversion, it says conformability error. Lets try again with mutually incompatible units.

You have: liter
You want: ohm
conformability error
0.001 m^3
1 kg m^2 / A^2 s^3

What it does is convert it to common base (m^n) and try to match them. But still, it doesn’t conform to standards conversions.

What if we are not sure of a unit or do not know how to spell them exactly. Just enter the first few letters and press tab, it shows a list of related units.

You have: kg
You want: l
Display all 102 possibilities? (y or n)
You want: li
li              lid             line            lithium
liang           light           linenyarncount  lithuanialitas
liberiadollar   lightminute     link            litre
libra           lightsecond     liquidbarrel
librae          lightyear       lira
libyadinar      ligne           liter

Another interesting feature of this tool is that we can obtain quick definitions of units. For example, if we want to know about ‘tablespoon’ then enter it in the “You have:” option and just press `Enter` without typing anything for “You want:” option. Then you get,

You have: tablespoon
You want:
Definition: ustablespoon = 1|16 uscup = 1.4786765e-05 m^3

Another good example will be definition of ohm and volt.

You have: ohm
You want:
Definition: V/A = 1 kg m^2 / A^2 s^3

You have: volt
You want:
Definition: W/A = 1 kg m^2 / A s^3

Thus, the interactive mode of GNU Units is very useful tool and we can play around with it. When we exactly know what we have and what we want, and just want the answer, the command line mode will be preferable. The basic syntax is,

$ units [options] [from-unit [to-unit]]

For example,

$ units '8 meter' 'inch'
* 314.96063
/ 0.003175

When the same command is invoked without the second ‘to-unit’ option, it just displays the definition of the given unit. For example,

$ units 'feet'
Definition: foot = 12 inch = 0.3048 m

Thus `units` is a versatile tool which converts quantities expressed in various scales to their equivalents in other scales. More information and options can be got from `man units`. The home page provides more information about the tool.

Units is available in Debian and Ubuntu, in the `universe` repository for the latter. So, its all an ‘apt-get install’ away. Thanks to lucas for posting this on the planet. Have fun! 🙂

Advertisements

7 thoughts on “GNU Units – for conversion between units

  1. the celcius fahrenheit conversion seems all WRONG! according to the degF degC conversion you have given that would mean 100 F is 55 C !! But actually its around 37 C (check google)…

    revi

    • “A long long time later” but…
      “The absolute temperature conversions are handled
      by units starting with `temp’, and you must use functional notation.
      The temperature differences are done using units starting with `deg’
      and they do not require functional notation.”

      You have: tempF(45)
      You want: tempC
      7.2222222

      You have: 45 degF
      You want: degC
      * 25
      / 0.04

      Again, more information see man pages or the home page.

  2. Thanks for pointing it out, but the error wasn’t mine because I just copied what I got as output in the terminal. So, it should be rather some error in the `unit` program itself. You’d be probably filing a bug against it 😉

  3. A long time later but…

    units does it correctly.

    degF to degC is for converting the difference between temperatures: this is correct as 1 degF = 5/9 degC

    Use tempF and tempC to convert temperatures (which will use (degC -32) * 5/9

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s