Repository navigation
Advanced Usage
Most of what there is to know about MarcusMedina.Units.Weight lives on the API Reference page. This page covers the parts that go beyond simple creation and conversion.
Weight is the single common representation (grams internally) for five otherwise-incompatible historical and modern systems: Metric, US, British, BritishOld (Troy/Apothecary), and SwedishOld. Because they all resolve to the same struct, values from different systems combine freely:
using MarcusMedina.Units.Weight.Metric;
using MarcusMedina.Units.Weight.British;
using MarcusMedina.Units.Weight.BritishOld;
Weight mixed = 1.Kilograms() + 1.Stone() + 1.TroyOunces();Several unit names appear in more than one namespace, and it matters whether they mean the same thing:
-
Grains()— identical (0.06479891 g) acrossBritish,US, andBritishOld(asTroyGrains()). Historically the grain was the one unit all these systems agreed on. -
Ounces()/Pounds()— identical betweenBritishandUS(28.349523125 g / 453.59237 g). Both use the same avoirdupois ounce/pound. -
Ounces()onBritishOld— there is no plainOunces()there; it'sTroyOunces()(31.1034768 g) andApothecaryOunces()(also 31.1034768 g, same value, different lineage) — deliberately distinct names from the avoirdupoisOunces(), because a Troy ounce is not an avoirdupois ounce. -
Tons diverge:
British.LongTons()(2240 lb, 1 016 046.9088 g) vs.US.ShortTons()(2000 lb, 907 184.74 g) are different methods on purpose — there's no bareTons().
As with the sibling Volume package, the rule is: if two systems disagree on a unit's size, the library gives them different method names rather than letting you guess which one you got.
Weight supports +, -, and scaling by a double via * / /. Dividing two Weight values (rather than a Weight by a double) returns a plain double ratio:
Weight doubled = 1.Stone() * 2;
double ratio = 1.LongTons() / 1.ShortTons(); // ≈ 1.12Weight implements IComparable<Weight> and IEquatable<Weight> plus the full comparison operator set, so a List<Weight> sorts correctly out of the box:
var weights = new List<Weight> { 1.Stone(), 1.TroyPounds(), 1.Skålpund() };
weights.Sort();Weight.ToString() always renders the underlying gram value in invariant culture, e.g. "453.59237 g", regardless of which unit the value was created from.