CACTI.Units 1.1.0

dotnet add package CACTI.Units --version 1.1.0
                    
NuGet\Install-Package CACTI.Units -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CACTI.Units" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CACTI.Units" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="CACTI.Units" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CACTI.Units --version 1.1.0
                    
#r "nuget: CACTI.Units, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CACTI.Units@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CACTI.Units&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=CACTI.Units&version=1.1.0
                    
Install as a Cake Tool

CACTI.Units

Manage units with conversions and mathematical operations.

Most code is generated through ISourceGenerator with AdditionalFiles for units descriptions.

Unit Dimensions

Units are defined by their base dimensions like (with international system and imperial units support):

  • Activity
  • Angle
  • Count
  • Current
  • Disk size
  • Distance
    • SI: Meter, Kilometer, ...
    • Imperial: Inch, Foot, Yard, Mile, ...
  • Duration
  • Energy
  • Force
  • Gravity
  • Mass
  • Radiation dose
  • Resistance
  • Revolution
  • Temperature
  • Voltage
  • Watt
  • Ratio

Composed unit dimensions

Some units dimensions are composed by other dimensions like:

  • Accelerations (Speed / Duration)
  • Count rate
  • Radiation rate
  • Revolution speed
  • Speeds (Distance / Duration)
    • SI: Meter per second, ...
    • Imperial: Mile per hour, ...
  • Radation rates (Dose / Duration)
  • Surface (Distance²)
  • Volume (Distance ^ 3)
    • SI: cubic meter, liter, ...
    • Imperial: Pint, Gallon, ...

Comparisons

Units of the same dimensions can be compared:

  • Equality (Equals or ==)
  • Difference ( != )
  • Superior ( > )
  • Inferior ( < )
  • Superior or equal ( >= )
  • Inferior or equal ( ⇐ )
Speed speed = (MeterPerSecond)10;
KilometerPerHour kilometerPerHour = 60;
Assert.IsTrue(kilometerPerHour > speed);
Assert.IsTrue(kilometerPerHour >= speed);
Assert.IsTrue(kilometerPerHour != speed);

MeterPerSecond meterPerSecond = 60;
kilometerPerHour = 216;
Assert.IsTrue(meterPerSecond >= kilometerPerHour);
Assert.IsTrue(meterPerSecond == kilometerPerHour);

Mathematical operations

All unit dimensions support the bellows operations:

  • Division (unit = unit / double)
  • Multiplication (unit = unit * double)
  • Addition (unit = unit + unit)
  • Substraction (unit = unit - unit)

The unit of an operation result is always the left operand unit.

SquareMeter squareMeter = 120;
Surface surface = squareMeter - (SquareMeter)30;
Assert.AreEqual("90 m2", surface.ToString());

Ratios operations

Along with mathematical operations bellow ratios operations are supported:

  • Ratio (ratio = unit / unit)
  • Division (unit = unit / ratio)
  • Multiplicaiton (unit = unit * ratio)
  • Addition (unit = unit + ratio)
  • Subsraction (unit = unit - ratio)
Ratio ratio = (MillisievertPerHour)50 / (MillisievertPerHour)200;
Assert.AreEqual("0.25", ratio.ToString());

MillisievertPerHour millisievertPerHour = (MillisievertPerHour)50 + (Percent)50;
Assert.AreEqual("75 mSv/h", millisievertPerHour.ToString());

Custom operators

Some units are extended with custom operators allowing conversions to another unit.

  • Speed = Distance / Duration
  • RadiationRate = RadiationDose / Duration
  • Surface = Distance * Distance
  • Volume = Surface * Distance
Volume volume = (Meter)5 * (Meter)5 * (Meter)2;
Assert.AreEqual("50 m3", volume.ToString());

SI units conversions to non SI

Some units are extended to support non SI conversions like volumes (exponent of distances) to liters:

CubicDecimeter cubicDecimeter = 5;
Liter liter = Liter.Convert(cubicDecimeter);
Assert.AreEqual("5 L", liter.ToString("0.###"));

liter += (Liter)5;
Assert.AreEqual("10 L", liter.ToString());

liter += (CubicDecimeter)3;
Assert.AreEqual("13 L", liter.ToString());

Serialization

All units can be serialized with formatting:

Meter distance = 50.555;
Assert.AreEqual("50.555 m", distance.ToString());
Assert.AreEqual("50.555 m", string.Format("{0}", distance));
Assert.AreEqual("50.55 m", distance.ToString("F2"));
Assert.AreEqual("50.55 m", string.Format("{0:F2}", distance));

Parsing

A text can be parsed with the

Speed speed = Speed.Parse("5 m/s");
Assert.AreEqual("5 m/s", speed.ToString());

KilometerPerHour kilometerPerHour = KilometerPerHour.Parse("5 m/s");
Assert.AreEqual("18 km/h", kilometerPerHour.ToString());
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
1.1.0 119 3/20/2026
1.0.0 139 3/3/2026