I am looking for a library that can handle unit conversions. I came across boost::units and unitscpp, however they only seem to cover simple units -- like length and mass. And they also do not seem to have much support in the way of decimal prefixes -- milli (m), giga (G), etc. I am looking for something more comprehensive, like the Python library Pint, that has the extensive units list, decimal prefixes and easy parsing of strings containing the value and the units to some usable object.
The Units program (now a GNU project) does just that, as a standalone program. It can do units conversion and simple arithmetic and understands SI prefixes (e.g. you can ask it how much
42 inches + 3 meters is in
mli). The set of units and prefixes is defined via a simple text file read at runtime, which is convenient if you need to fine-tune it (e.g. to update currencies based on the current rates). It's portable C and should work just about anywhere.
You can call
units as a standalone program and retrieve its output via
read with via Boost.Process. Pass the
-t option, the original value and the target unit (omit that if you want to convert to the standard unit). This has a significant overhead; if you're going to do a lot of conversions, launch one instance of
units, pass it conversion requests on its standard input, and read back the result from its standard output.
While the program doesn't come with a librar, you can tweak the code a little and link it into your program. Omit the
main function, change the
printf calls into
sprintf, and call
showanswer directly. That's a bit of maintenance work, but the code doesn't change often, and if you're lucky the upstream author will accept your patches to build a separate library. Do note that the code is licensed under the GNU GPL, so if you link it directly into your program, your code will have to be licensed under the GNU GPL as well (if you call
units as an external program, there is no license constraint).
If all else fails you can always embed python and Pint into your C/C++ program and use them from within the C/C++ code. If you do this you will have access to python and it's libraries from anywhere in your code.
See https://docs.python.org/2/extending/embedding.html for details.
I'm just looking for the same kind of units-conversion library as you, but I also have the additional requirement that the license cannot be copyleft. After some search, I found UDUNITS, which isn't copyleft and moreover even has an API to use it as a library. I also found that the original
units program wasn't copylefted before the GNU Units implementation. The original version can still be found if you search, but however, I find the UDUNITS library very interesting and I feel it's just what I'm looking for.
Also of interest: The FreeBSD implementation of the
units program. Reading at the commits comments in the source repository, it seems they try to get close to the GNU units behavior, while not being copylefted. You can browse the repository (the link points to the r11 FreeBSD release).