DoenaSoft.UnitsOfMeasurement
1.3.0
See the version list below for details.
dotnet add package DoenaSoft.UnitsOfMeasurement --version 1.3.0
NuGet\Install-Package DoenaSoft.UnitsOfMeasurement -Version 1.3.0
<PackageReference Include="DoenaSoft.UnitsOfMeasurement" Version="1.3.0" />
paket add DoenaSoft.UnitsOfMeasurement --version 1.3.0
#r "nuget: DoenaSoft.UnitsOfMeasurement, 1.3.0"
// Install DoenaSoft.UnitsOfMeasurement as a Cake Addin #addin nuget:?package=DoenaSoft.UnitsOfMeasurement&version=1.3.0 // Install DoenaSoft.UnitsOfMeasurement as a Cake Tool #tool nuget:?package=DoenaSoft.UnitsOfMeasurement&version=1.3.0
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>(6m)).Add(new Value<Inch>(4m)); // 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));
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 LitersPer100km = 5.9 / 100;
var source = new Value<FractionUnit<Liter, Kilometer>>(LitersPer100km);
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 was computed. 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 was computed. 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 | netcoreapp1.0 was computed. netcoreapp1.1 was computed. netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 is compatible. netcoreapp3.0 was computed. netcoreapp3.1 is compatible. |
.NET Standard | netstandard1.3 is compatible. netstandard1.4 was computed. netstandard1.5 was computed. netstandard1.6 was computed. netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net452 is compatible. net46 is compatible. net461 was computed. net462 is compatible. net463 was computed. net47 is compatible. 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 | tizen30 was computed. tizen40 was computed. tizen60 was computed. |
Universal Windows Platform | uap was computed. uap10.0 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 2.2
- System.ComponentModel.Annotations (>= 4.5.0)
-
.NETCoreApp 3.1
- System.ComponentModel.Annotations (>= 4.5.0)
-
.NETFramework 4.5.2
- System.ComponentModel.Annotations (>= 4.5.0)
-
.NETFramework 4.6
- System.ComponentModel.Annotations (>= 4.5.0)
-
.NETFramework 4.6.2
- System.ComponentModel.Annotations (>= 4.5.0)
-
.NETFramework 4.7
- 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 1.3
- NETStandard.Library (>= 1.6.1)
- 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)
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 | 93 | 7/10/2024 |
2.0.1 | 830 | 11/28/2022 |
2.0.0 | 867 | 10/24/2022 |
1.5.3 | 884 | 8/21/2022 |
1.5.2 | 874 | 8/21/2022 |
1.5.1 | 921 | 4/27/2022 |
1.5.0 | 930 | 3/12/2022 |
1.4.0 | 883 | 3/12/2022 |
1.3.0 | 866 | 3/12/2022 |
1.2.2 | 859 | 3/11/2022 |
1.2.1 | 931 | 3/11/2022 |
1.2.0 | 922 | 3/11/2022 |
1.1.1 | 902 | 3/11/2022 |
1.1.0 | 889 | 3/11/2022 |
1.0.5 | 927 | 3/11/2022 |
1.0.4 | 901 | 3/11/2022 |