UnitConvert is an API that we created that makes it easy to convert any plain text measurement to another compatible measurement, get information about the measurement, or compare with another measurement.
Why is this useful? Here are a few scenarios where UnitConvert could be used:
Personally, I think the use case for speech recognition of units is particularly compelling, as UnitConvert could be integrated with a solution such as Xamarin for cross platform apps to recognize spoken measurements.
It’s easy to get started with UnitConvert in your projects. Not only can you call the API, but there is also a NuGet Package available and a Composer Package available for those working in PHP frameworks like Laravel.
Using the UnitConvert NuGet Package
Let’s explore UnitConvert in the context of a .NET Core console application, just to see what we can do. You can do this in Visual Studio 2017 by going to File –> New –> Project and selecting .NET Core –> Console App.
To be able to work with UnitConvert, you can just install the UnitConvert NuGet package and you’re ready to roll. Note that this package is built against .NET Standard 1.4+, but also available for .NET Framework 4.5+ projects as well.
You also need a UnitConvert API key, which you can get for free from here: https://unitconvert.io. If you’d rather call the APIs directly, you can certainly do that as well, documentation of available endpoints is available on the UnitConvert site.
Next, create a new instance of “UnitConvertClient”, and supply your API Key in the constructor. You’ll have to add a “using UnitConvert” refence at the top of your class to be able to do this:
UnitConvertClient client = new UnitConvertClient(“YOUR_API_KEY”);
There are three methods available to us in the client
Info: var data = await client.GetMeasurementInfoAsync(“10.352 inches”);
Conversion: var data = await client.ConvertMeasurementAsync(“12g”, ” milligrams”);
Comparison: bool result = await client.CompareMeasurementsAsync(“11 inches”, ComparisonType.GreaterThan, “4cm”);
Note that there’s no consistency to the way the units above are formatted – there’s abbreviations, full units, some with spaces, some without. They’ll all just work. A complete code sample implementing all of these can be seen below:
The above code calls each of the available methods, and prints the results to the console. The output of looks like this:
The GetMeasurementInfoAsync and ConvertMeasurementAsync methods both return an instance of the MeasurementInfo class, which is structured like this:
There is a lot of useful data here in addition to what we accessed above, including what name variants the unit has (e.g. mg, milligrams, milligram, etc), and what other units this unit can be converted to.
That’s the gist of UnitConvert! One of the great things about it is it’s always improving – any failed conversions or lookups are logged and analyzed to ensure they don’t fail again if they are legitimate. Try it out today – https://unitconvert.io.