Skip to main content

One post tagged with "units"

View All Tags

UnitsNet

· 3 min read
Ahmet Buğra Kösen
Software Developer

Unit conversions can be challenging in applications working with physical quantities. The UnitsNet library, developed for the .NET platform, makes conversions between different measurement systems and physical quantities easy and reliable. In this article, we'll explore the features and use cases of the UnitsNet library.

What is UnitsNet?

UnitsNet is an open-source library developed to perform unit conversions of physical quantities in .NET applications in a simple and reliable way. It supports many measurement types and enables conversions between different units. For example, operations like getting a length in meters as kilometers or converting a weight in kilograms to pounds are extremely easy with UnitsNet.

Supported Unit Types

The UnitsNet library supports a wide range of physical quantities and units. These quantities include:

  • Length: meter, kilometer, mile, inch, foot, etc.
  • Mass: kilogram, gram, ton, pound, etc.
  • Temperature: Celsius, Fahrenheit, Kelvin, etc.
  • Volume: liter, milliliter, gallon, cubic meter, etc.
  • Area: square meter, hectare, acre, etc.
  • Pressure: Pascal, bar, atm, psi, etc.
  • Speed: meter/second, kilometer/hour, mile/hour, etc.
  • Energy: joule, calorie, kilowatt-hour, etc.
  • Power: watt, kilowatt, horsepower, etc.
  • Information: byte, kilobyte, megabyte, gigabyte, terabyte, etc.

UnitsNet provides support for the above quantities and more, making it suitable for a wide variety of engineering and scientific calculations.

UnitsNet Implementation

First, let's add the UnitsNet NuGet package to the project:

dotnet add package UnitsNet

Defining a quantity and converting it to different units with UnitsNet is quite simple:

using UnitsNet;

class Program
{
static void Main()
{
// Define 10km
var distance = Length.FromKilometers(10);

Console.WriteLine($"Meters: {distance.Meters}");
// Meters: 10000

Console.WriteLine($"Miles: {distance.Miles}");
// Miles: 6.2137119223733395

Console.WriteLine($"Yard: {distance.Yards}");
// Yard: 10936.132983377078


// Define 100 MB
var fileSize = Information.FromMegabytes(100);

Console.WriteLine($"Byte: {fileSize.Bytes}");
// Byte: 100000000

Console.WriteLine($"Gigabyte: {fileSize.Gigabytes}");
// Gigabyte: 0.1
}
}

In this code, we define a length of 10 kilometers with Length.FromKilometers(10) and display its values in different units using properties like distance.Meters and distance.Miles. We also convert a 100 MB file size to bytes and gigabytes.

For more information, please check out the project's GitHub page...

Things to Consider When Using UnitsNet

It's useful to pay attention to the following points during usage:

  1. Choosing the Right Quantity: There's a separate class for each quantity (like Length, Mass, Temperature). Make sure you choose the correct quantity to use.
  2. Unit Precision: UnitsNet may perform rounding in some unit conversions. If you're performing operations that require very high precision, it's worth checking the results.
  3. Performance: When working with large datasets, UnitsNet conversion operations may need to be optimized. It's especially beneficial to run performance tests if unit conversions will be done inside large loops.

Conclusion

UnitsNet is a great solution for developers who want to perform unit conversions reliably and easily on the .NET platform. With its wide unit support, simple usage, and powerful conversion features, it provides convenience in scientific, engineering, and everyday applications. If you have unit conversion needs in your projects, I recommend trying UnitsNet.

See you in the next article...