Fun With Units

Converting between units is a task that comes up occasionally and can get tricky especially if you want to script the conversions.

Enter the command line tool; units.

This fantastic little tool allows the conversion between any compatible units with ease. It can be used either interactively (which is how we will use it here) or it will accept standard input.

Once you have installed it simply run it without nay options to open the interactive shell:

~]$ units
Currency exchange rates from finance.yahoo.com on 2018-06-25
3050 units, 109 prefixes, 109 nonlinear units

You have:

As you can see it is capable of converting 3050 units as August 2018. Obviously, it can do super simple stuff like converting between Metric and Imperial/English/Oldtimey units. E.g.:

You have: 1 yard
You want: cm
        * 91.44
        / 0.010936133

The first line of output, starting with a * is the answer, and the second line, starting with a / is the calculation in the opposite direction i.e. 1 cm is a little under 0.01 of a yard.

The inverse is useful particularly where the first unit is smaller than the second e.g.:

You have: 1 meter
You want: furlong
        * 0.0049709695
        / 201.168

1 meter is 0.005 of a furlong which is much less intuitive than finding out there that are 201 meters in a furlong.

So how about some fun conversions? This was a classic moment from The Simpsons when Grandpa Abe mentions the fuel efficiency of his car:

Let’s put those numbers into units:

You have: 40 rods/hogshead
You want: km/litre
        * 0.00084287188

0.00084287188 of a kilometre is a little hard to picture so lets use units again and find out how many cm that is:

You have: 0.00084287188 km
You want: cm
       * 84.287188

That means that Abe’s car goes about a 84cm per litre of fuel. And in inches per gallon:

You have: 0.00084287188 km / litre
You want: inches / gallon
        * 150.85714

or 150 inches per gallon.

Next, how fast does data travel across the internet compared to the speed of light?

NY is located about 5551 km away from where I’m sitting in the UK which makes it a round trip (which is what a ping command is measuring) of 11103 km. If we imagine bouncing a laser off a mirror on the Empire State Building it would take:


You have: 11103 km
You want: milli light seconds
        * 37.035621

37 milliseconds to complete the round trip at the speed of light.

Next, I found a server located in NY and pinged it. The best results were clustered around around 77ms.

This means that the internet is only around 50% slower than the speed of light. Which isn’t too bad considering that the path is far from a straight line and some of that distance is travelling along copper.

Finally, you can use units for all of those pesky times you need to convert data quantities from amounts like 8TB per month to KBp/s. It’s common for hosting companies to allocate a total quantity of transmitted and received data to a server in a month but what does that equal in seconds or days?

Imagine your hosting provider allocates your VPS 5TB per month of total transfer. What does this equal in data / second?

You have: 5 terabytes / month
You want: megabytes / second
        * 1.9013259

Or, conversely, you may be allocated 5Mbps. How much data can you transmit in a day?

You have: 5 megabits / second
You want: gigabytes / day
        * 54

This tool makes it easy to convert between bits and bytes per seconds or months.