A simple, dynamic library for units of measure.
$ dotnet add package Tareuser guide | api documentation | changelog
Tare is a .NET library that provides a Quantity type for working with physical quantities and units of measurement. Unlike other units libraries, Tare is built around runtime unit parsing, conversion, and supports dimensional arithmetic.
It supports:
Quantity type.using Tare;
//Implicit creation and explicit parsing
Quantity length = "1.5 m";
var force = Quantity.Parse("13 lbf");
//support for scalars
Quantity scalar = 2;
var scalar2 = Quantity.Parse("2");
// TryParse operations
if (Quantity.TryParse("3.2 ft/s^2", out var acceleration))
{
// Success
}
// creation from another quantity (with conversion)
var torque2 = torque.As("lbf*in");
Tare automatically handles dimensional algebra when multiplying or dividing quantities:
// Perform some simple math
var longLength = length * scalar;
var longLength2 = length * 2;
var torque = force * length;
var area = length * length;
// Velocity from distance and time
var distance = Quantity.Parse("100 m");
var time = Quantity.Parse("9.58 s");
var velocity = distance / time; // Result: 10.44 m/s
Console.WriteLine(velocity.Format("m/s")); // "10.44 m/s"
// Force calculation (F = ma)
var mass = Quantity.Parse("5 kg");
var acceleration = Quantity.Parse("2 m/s^2");
var force = mass * acceleration; // Result: 10 N (Newtons)
Console.WriteLine(force.Format("N")); // "10 N"
Tare prevents invalid operations at runtime by checking dimensional signatures:
// This throws InvalidOperationException - can't add different dimensional signatures!
var invalid = torque + area; // Error!
Support for converting between units:
///Unit conversion and string formatting
Console.WriteLine(force.Format("N")); // "57.7883 N"
Console.WriteLine(force.Format("ozf")); // "2080 ozf"
Support for standard dotnet numerical string formatting:
Console.WriteLine(torque.ToString("F1"); // "8. J"
// Compare quantities
if (longLength > length)
Console.WriteLine("longLength is greater than length");
if (longLength == long)
Console.WriteLine("longLength is equal to long");
Tare supports a wide variety of units across many dimensions:
Available on NuGet: Tare
dotnet add package Tare
Tare maintains rigorous validation of unit conversion accuracy through a dedicated test project and comprehensive documentation. All conversion factors are independently verified against authoritative sources including NIST SP 811, BIPM SI standards, and ISO 80000-1.
Unit Validation Strategy - Complete methodology for ensuring conversion accuracy
Unit Accuracy Audit Report - Comprehensive accuracy audit with confidence intervals
Validation Test Project - Dedicated test project (Tare.UnitValidation.Tests)
All unit conversions are validated against exact definitions from international standards:
Test Results: 625 total tests (573 functional + 52 validation) with 100% pass rate ✅
Test Coverage: 93.25% line coverage, 67.25% branch coverage (exceeds 85% target) ✅
# Run all tests
dotnet test
# Run with coverage (requires dotnet-coverage tool)
dotnet tool install -g dotnet-coverage
dotnet-coverage collect -f cobertura -o coverage.cobertura.xml dotnet test
# Generate HTML coverage report (requires reportgenerator tool)
dotnet tool install -g dotnet-reportgenerator-globaltool
reportgenerator -reports:coverage.cobertura.xml -targetdir:coverage-report -reporttypes:Html
# Run specific test category
dotnet test --filter "FullyQualifiedName~S004TestMatrixTests"
The test suite includes:
For those interested in learning more about dimensional analysis and units of measure: