UnitsNet is a library for .Net that greatly simplifies working with variables with units representing physical magnitudes.
You have surely suffered the little hell that results from having to manage dimensions, surfaces, mass measurements… when different units are involved, and how cumbersome it is to take into account all possible considerations.
Units.Net provides the functions to handle magnitudes. It incorporates more than 1000 units of 95 types of physical magnitudes, such as length, mass, force, time, pressure.
Creating a physical magnitude in Units.Net is as simple as starting it from one of the static methods
Length meter = Length.FromMeters(1);
Length twoMeters = new Length(2, LengthUnit.Meter);
From there, we can operate with them, and Units.Net will perform the appropriate treatment for them.
Length l1 = 2 * Length.FromMeters(1);
Length l2 = Length.FromMeters(1) / 2;
Length l3 = l1 + l2;
Of course, we can perform more complex operations involving several units. Units.Net will throw an exception if any of the operations are not consistent with its units.
Length distance = Speed.FromKilometersPerHour(80) * TimeSpan.FromMinutes(30);
Acceleration a1 = Speed.FromKilometersPerHour(80) / TimeSpan.FromSeconds(2);
Acceleration a2 = Force.FromNewtons(100) / Mass.FromKilograms(20);
RotationalSpeed r = Angle.FromDegrees(90) / TimeSpan.FromSeconds(2);
Units.Net incorporates many more interesting functions, which cover most of the needs we have when working with unit variables. These functions include conversion, parsing, serialization, string conversion in various languages.
In addition, it is possible to extend with our own units, including a definition in a Json file. Although the library incorporates most of the units you may need in your projects.
Units.Net is Open Source and the code is available at https://github.com/angularsen/UnitsNet. In addition, it is available in a Nuget package to easily add it to our projects, and is compatible with .Net Framework and .Net Standard.