Skip to content

DJDoena/DoenaSoft.UnitsOfMeasurement

Repository files navigation

This package allows the conversion of common SI and American units within the same category (area, energy, length, temperature, time, volume, weight/mass)

All units can be accessed by type name (e.g. Meter) or serializable / SI value (e.g. "m");

Sample conversion from Kilogram to pound:

var lb = new Value<Pound>(1);

var kg = ValueConverter.Convert<Kilogram>(lb).Round(9);

Assert.AreEqual(0.45359237, kg.Scalar);

kg = new Value<Kilogram>(1);

lb = ValueConverter.Convert<Pound>(kg).Round(9);

Assert.AreEqual(2.204622622, lb.Scalar);

It also allows the registration of custom units (i.e not pre-defined in this assembly) with a conversion factor to the category's base unit.

var custom = new CustomWeight(0.0311034768, "oz.tr."); // ounce

UnitConverter.RegisterCustomUnit(custom);

var result = UnitConverter.ToUnitOfMeasurement("oz.tr.");

It also alows the creation of fractional units such as meters per second

var mps = UnitConverter.ToUnitOfMeasurement("m/s");

You can also add values from the same unit category to each other without converting the units first:

var source = new Value<FractionUnit<Meter, Second>>(30);

var target = source.Add(new Value<FractionUnit<Mile, Hour>>(15));

Or calculate Superman's height in meters:

var sourceFeet = new Value<Foot>(6).Add(new Value(4, "inc")); // 6'4"

var targetMeter = ValueConverter.Convert<Meter>(sourceFeet).Round(2); // 1.93m

You can also convert between volume and weight units with a given density.

const double DensityOfHelium = 0.1785; // kg/l

var source = new Value(5, UnitConverter.ToUnitOfMeasurement("dm3"));

var target = ValueConverter.Convert(source, new Kilogram(), new DensityValue<Density<Kilogram, Liter>>(DensityOfHelium)); // 0.8925kg

You can even convert values with fractional units where the unit categories are inverted, such as liter per 100 km (volume/length) to gasmileage (length/volume)

const double LitersPerKm = 5.9 / 100;

var source = new Value<FractionUnit<Liter, Kilometer>>(LitersPerKm);

var target = ValueConverter.Convert(source, new FractionUnit<Mile, USLiquidGallon>()).Round(5); // 39.86688 miles per gallon

More examples can be found in the unit tests for the project

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages