Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Free units conversion app in OS X

UNIXNot sure if this is exactly news, but it's a big help for any other students in science classes out there or anyone else who needs to occasionally convert picometers to fathoms. In the terminal, just type units, which is a UNIX app that knows how to convert between 501 different units of measurement, everything from inches to furlongs to stones.

[Editor's note: Type "man units" to get a couple of useful pages that explain how to use the program. Note that it will not do Fahrenheit/Celcius, but it looks like it can handle almost anything else!]

Free units conversion app in OS X | 8 comments | Create New Account
Click here to return to the 'Free units conversion app in OS X' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Can't do temperatures? Preposterous!
Authored by: babbage on Jun 01, '01 11:36:44AM
Why, just take a look at Mastering Regular Expressions, page 199. It's a Perl one-liner!
11:17:54am :chris% echo 73F>temp.txt
11:18:21am :chris% cat temp.txt
11:18:25am :chris% perl -pi -e 's[(d+(.d*)?)Fb]{sprintf "%.0fC", ($1-32) * 5/9}eg' temp.txt
11:20:18am :chris% cat temp.txt
11:20:21am :chris%

Some slight editing of the printf part should give you conversions the other way too:

11:27:58am :chris% echo 73F > temp.txt
11:28:01am :chris% cat temp.txt
11:28:09am :chris% perl -pi -e 's[(d+(.d*)?)Fb]{sprintf "%.0fC", ($1-32) * 5/9}eg' temp.txt
11:28:17am :chris% cat temp.txt
11:28:19am :chris% perl -pi -e 's[(d+(.d*)?)Cb]{sprintf "%.0fF", ($1 * 9 / 5) + 32}eg' temp.txt
11:28:28am :chris% cat temp.txt
11:28:30am :chris%

Of course this is mainly only useful if you're insane, but then hey if you are then welcome to the world of Perl scripting... :)

[ Reply to This | # ]

Am I missing something?
Authored by: sjonke on Jun 01, '01 03:36:35PM

All I seem to be able to get this to do is to print out HOW to do the conversion, not to actually do one. Is there any way to get this to actually DO a conversion? The man page is less than helpful in this area.

[ Reply to This | # ]
Am I missing something?
Authored by: babbage on Jun 01, '01 04:17:35PM
Hmm. Well, in the Unix spirit of things, maybe this is just meant to serve as a filter for other programs. Given a pair of input units, you can get it to produce regular, machine readable output:
4:05:12pm :chris% units feet meters
        * 0.3048
        / 3.2808399
4:05:22pm :chris% units years seconds
        * 31556926
        / 3.1688765e-08
4:05:30pm :chris% units liters gallons
        * 0.26417205
        / 3.7854118
4:05:49pm :chris%
Maybe you could mess around with those result lines to do a conversion. For example, something like: "units [from] [to] | head -1" gives you your mathematical operator (whichever of multiplication or division, and the number to calculate against). You could then embed this within "printf" or (more flexibly) some Sed, Awk, or Perl code.

In any event, I think you're right -- this doesn't seem to be an all-purpose tool for making conversions so much as the raw engine for one, but like an engine with no wheels, that's not very useful without quite a bit of extra work. Too bad.

On the brighter side, if you *do* want to extend it, it looks like you can add more conversions to the data file: /usr/share/misc/units.lib. I don't totally understand the format, but it looks like you should be able to go in (as root) and add, if nothing else, the values for fahrenheit & celcius conversions. Considering a lot of the other ones in there (e.g. kelvin), I'm surprised that those two were left out. Maybe it's the proverbial "exercise left to the reader".....

[ Reply to This | # ]

Am I missing something?
Authored by: tanderson4 on Jun 02, '01 05:08:44PM

From experimenting a bit, it appears that there are at least a couple of ways to get the units program to do conversions. If you want to be prompted, run units and then enter the values along with the units to be converted, like this:

% units
You have: 5.8 feet
You want: cm
* 176.784
/ 0.0056566205
You have: 160 lbs
You want: oz
* 2560
/ 0.000390625

If you want to use the command line, do this:

% units 5.8-feet cm
* 176.784
/ 0.0056566205
% units 160-lbs oz
* 2560
/ 0.000390625

The first number returned is the conversion. Still trying to figure out what the second number (after the "/") is....

[ Reply to This | # ]
Am I missing something?
Authored by: babbage on Jun 02, '01 06:02:55PM
Whether invoked interactively or all via the command line, the syntax works out the same. You give a value, and units A & B. The response line tells you how to get from A to B, while the second line tells you how to get from B to A. So in your example, you have to multiply by 176.786 if you want to go from feet to centimeters, and the second line tells you that you have to divide by 0.0056566205 to get from centimeters to feet. Both lines are giving you "the conversion"; the first line gives you A -> B and the second line gives you B -> A. If you always invoke the command with the units you have first and the units you want second (which makes more sense to me), then you'll always just be interested in the first line.

No wait I'm horribly wrong.

Upon closer inspection, the first line is always multiplication and the second line is always division. The numbers are mutually reciprocal: using liters & gallons, you get * 0.26... and / 3.78..., and if you take either of those numbers and divide one by it, you get the other: (3.78...) = (1 / 0.26...) && (0.26...) = (1 / 3.78...). SO, what you have here are two numbers that can get you from units of A to units of B. The only difference, as near as I can tell, is that you have a choice regarding whether you prefer multiplying or dividing to get your result.

I have no idea why this would matter to anyone. Presumably some electrical engineer wrote this command back in the day, and it would be useful in that context as part of a larger script, as a database of conversion factors for arbitrary calculations. Considering that it doesn't actually do the calculations, but rather helps describe how to write the formula for the calculations, I can't see how this would be useful for most people. Some intrepid soul could probably extend this program in order to do that, but short of that, it looks like you still have to do the (easier, more mechanical part of the) work yourself...

[ Reply to This | # ]

Am I missing something?
Authored by: rusto on Jun 03, '01 12:09:33AM

You have: 2 feet
You want: meters
* 0.6096 <----this is the answer
/ 1.6404199 <----this is how to get back to where you started
You have: 3 feet
You want: meters
* 0.9144
/ 1.0936133

So I would say it DOES figure out the answer for you. Note that in the second example above, the answer is's not just showing a conversion factor. Potentially handy but someone probably has something like unit conversion incorporated into an OSX calculator.

[ Reply to This | # ]
Invoking UNIX Commands
Authored by: Gabriel Birke on Jun 04, '01 03:55:39PM


I heard somewhere that it is possible to invoke UNIX commands from RealBasic and collecting the output from the commands. Does anybody know how? Then you could build your conversion app on top of unit.

AppleScript, C or Java source would help also.

[ Reply to This | # ]
Invoking UNIX Commands
Authored by: Gabriel Birke on Jun 05, '01 01:35:46PM

Ok, now I know how to do it in RealBasic (The "Shell" command).
And I explored the files in /usr/share/misc/ where the units.lib file is located. Did you know that there are other interesting files there? For example the file "flowers" shows you the symbolic meaning of flowers. There is an ascii table, all zip codes of the USA and much more ...

[ Reply to This | # ]