DoenaSoft.UnitsOfMeasurement
2.0.2
dotnet add package DoenaSoft.UnitsOfMeasurement --version 2.0.2
NuGet\Install-Package DoenaSoft.UnitsOfMeasurement -Version 2.0.2
<PackageReference Include="DoenaSoft.UnitsOfMeasurement" Version="2.0.2" />
paket add DoenaSoft.UnitsOfMeasurement --version 2.0.2
#r "nuget: DoenaSoft.UnitsOfMeasurement, 2.0.2"
// Install DoenaSoft.UnitsOfMeasurement as a Cake Addin #addin nuget:?package=DoenaSoft.UnitsOfMeasurement&version=2.0.2 // Install DoenaSoft.UnitsOfMeasurement as a Cake Tool #tool nuget:?package=DoenaSoft.UnitsOfMeasurement&version=2.0.2
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
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net452 is compatible. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 is compatible. net48 is compatible. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.5.2
- System.ComponentModel.Annotations (>= 4.5.0)
-
.NETFramework 4.7.2
- System.ComponentModel.Annotations (>= 4.5.0)
-
.NETFramework 4.8
- System.ComponentModel.Annotations (>= 4.5.0)
-
.NETStandard 2.0
- System.ComponentModel.Annotations (>= 4.5.0)
-
.NETStandard 2.1
- System.ComponentModel.Annotations (>= 4.5.0)
-
net5.0
- System.ComponentModel.Annotations (>= 4.5.0)
-
net6.0
- System.ComponentModel.Annotations (>= 4.5.0)
-
net7.0
- System.ComponentModel.Annotations (>= 4.5.0)
-
net8.0
- System.ComponentModel.Annotations (>= 4.5.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.0.2 | 106 | 7/10/2024 |
2.0.1 | 835 | 11/28/2022 |
2.0.0 | 872 | 10/24/2022 |
1.5.3 | 889 | 8/21/2022 |
1.5.2 | 879 | 8/21/2022 |
1.5.1 | 928 | 4/27/2022 |
1.5.0 | 937 | 3/12/2022 |
1.4.0 | 890 | 3/12/2022 |
1.3.0 | 873 | 3/12/2022 |
1.2.2 | 866 | 3/11/2022 |
1.2.1 | 938 | 3/11/2022 |
1.2.0 | 929 | 3/11/2022 |
1.1.1 | 909 | 3/11/2022 |
1.1.0 | 896 | 3/11/2022 |
1.0.5 | 934 | 3/11/2022 |
1.0.4 | 908 | 3/11/2022 |