Search

gnu units: units converts quantities between different scales

July 4th, 2007 edited by lucas

Article submitted by Geoffroy Youri Berret. We are running out of articles ! Please help DPOTD and submit good articles about software you like !

units is a command-line tool which performs units conversion between various scales. units is the perfect tool for engineer or scientific workstation, you can call it either interactively from the prompt or within command line. It already handles more than two thousand units and this can be enhanced with your own units in a separate data file.

units handles multiplicative scales factor as well as non-linear conversions units such as Celsius Fahrenheit. Compound units are also allowed in order to deal with quantities such as speed, volume, energy, etc.

Interactive use

Calling units without any option will run it interactively. Here is an example showing how to convert cm3 to gallons:

%units
2438 units, 71 prefixes, 32 nonlinear units

You have: cm^3
You want: gallons
        * 0.00026417205
        / 3785.4118

units returns two conversion rates. The first one is the convertion factor you asked for, the second is its inverse or the conversion in the oposite direction. Sometimes the inverse factor could be more convenient because it would be the exact value.

units also provides the definition of units of you leave ‘You want:’ field empty:

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

Or you may want to compute sums of conformable units:

You have: 2 hours + 46 minutes + 40 seconds
You want: seconds
        * 10000
        / 0.0001
You have:  20 inches + 15 cm - 1 foot
You want: cm
        * 35.32
        / 0.028312571

And finally, not a minor feature when you have to deal with so many units, the completion with the tab key. It will complete unitname if there is a unique way to do so or provide a list of possibility with the second hit of the tab key :

     You have: metr
     metre             metriccup         metrichorsepower  metrictenth
     metretes          metricfifth       metricounce       metricton
     metriccarat       metricgrain       metricquart       metricyarncount
     You have: metr

units non-interactively

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

Invoking units with options will turn off the interactive mode and return conversion to stdout :

%units '2 liters' 'pints'
        * 4.2267528
        / 0.23658824

Availability

gnu units has been available in Debian since Sarge and in Ubuntu since Warty.

Links

Posted in Debian, Ubuntu |

7 Responses

  1. Wim Coenen Says:

    People who need such a tool will also be interested in knowing this:

    If you work with a browser open, then you already have a very good tool to do stuff like this. Try googling these:

    2 pints in liter
    20 inches + 15 cm - 1 foot in cm
    1.4 euro per liter in dollars per gallon

    (And yes, that last one is really what we pay for gas in Belgium…)

  2. erik Says:

    Just use Google.

  3. Erwan Says:

    And google knows how to convert liter per 100 km into miles per gallon, which units cannot do

  4. mathew Says:

    I’ve never understood why units doesn’t actually do the computation. I mean, it’s a computer program, if it knows how to convert lbs to kg, why can’t it just do it for me? Why do I have to load another program to do that?

  5. geoffroy Says:

    Well Mathew, I think that is part of Unix Philosophy actually :

    “Make each program do one thing well. To do a new job, build afresh rather than complicate old programs by adding new features.” Doug McIlroy

    Anyway, I understand one might expect more from this kind of program. I’ve heard about qalc which is doing conversion and computation. And that’s Free Software Philosophy : choice and liberty to choose :)

  6. Reed Says:

    Useful for shell scripts maybe (though it ought to just do the computation, without needing to cut up the output) but I’ll probably stick with Google and reference tables in books.

  7. Jon Says:

    I’d never heard of this before.
    Thanks for including it.