Repository navigation
Getting Started
dotnet add package MarcusMedina.Units.WeightRequirements: .NET 10.0+, C# 14.0+
Weight is a readonly struct that always stores its value internally as grams. Every unit family (Metric, US, British, BritishOld, SwedishOld) lives in its own namespace and adds two kinds of extension methods:
- A creation extension on
int/double, e.g.11.Stone()or2.5.Kilograms(), which produces aWeight. - A conversion extension on
Weight, e.g.weight.ToPounds()orweight.ToSkålpund(), which returns a plaindouble.
Because the underlying storage is always grams, you can freely mix units from different systems in the same expression — 2.5.Kilograms() + 1.Pounds() just works, no manual conversion required. You only need to using the namespace(s) for the units you actually want to create or read; the Weight struct itself lives in the root MarcusMedina.Units.Weight namespace.
Arithmetic (+, -, *, /) and full comparison operators (<, >, ==, etc.) are supported directly on Weight values.
using MarcusMedina.Units.Weight.Metric;
using MarcusMedina.Units.Weight.British;
// Create weights from different unit systems
Weight parcel = 2.5.Kilograms();
Weight person = 11.Stone();
// Convert to whichever unit you need to display
double parcelInPounds = parcel.ToPounds(); // ≈ 5.51
double personInKg = person.ToKilograms(); // ≈ 69.85
// Mixing units in arithmetic is always safe —
// Weight is stored internally in grams regardless of the unit used to create it
Weight combined = parcel + 1.Pounds();
// Comparisons work directly on Weight values
bool personIsHeavier = person > parcel;Next: see the API Reference for the full list of supported units across all five unit families.