equadrat.Framework.Math.Uom.Ucum (8.3.6)

Published 2026-09-09 21:42:55 +02:00 by frederik.engelhardt

Installation

dotnet nuget add source --name equadrat-oss --username your_username --password your_token 
dotnet add package --source equadrat-oss --version 8.3.6 equadrat.Framework.Math.Uom.Ucum

About this package

A framework to extend the Microsoft .net framework with extra math functionality.

equadrat Framework Math

The idea of the equadrat Framework is to provide APIs to build applications with an architecture of highly decoupled components. Every single component, aspect or pattern is represented by interfaces. The framework provides a standard implementation for these but each type can be replaced by a custom implementation.

The framework is not about reimplementing components, but making them available using common interfaces.

Getting started

You can use most of the components by simply instantiating them. This is maybe the easiest way to evaluate the framework.

If you're using an IOC/DI container such as Microsoft Extensions DependencyInjection or if you want to use the built-in IOC framework, you can use the bootstrapper and bootstrapper modules to register the framework to the IOC.

Usage

Register to IOC/DI container

public static void Main(string[] args)
{
    var builder = Host.CreateApplicationBuilder(args);

    // Use the module registry to register bootstrapper modules. Usually each assembly has its own bootstrapper module.
    // This will add their service declarations to the IServiceCollection.
    // This requires equadrat.Framework.Core.Microsoft.Extensions.Hosting NuGet package.
    builder.Services.RegisterBootstrapperModules(moduleRegistry => moduleRegistry.MathFramework().RegisterUomUcumModule());

    // Declare other services as usual.
    // builder.Services.AddSingleton<...>(...);

    var app = builder.Build();

    // Optional: initialize all services declared by the registered bootstrapper modules. Calling this method is required for all environments which doesn't support IHostedService, such es browsers/webassembly.
    app.Services.InitBootstrapperModules();

    app.Run();
}

Serializing units using the UCUM standard

IServiceProvider serviceProvider = ...;

var units = serviceProvider.GetRequiredService<IMathUnits>().GetGeneric<eMathScalePrefix, int>();
var unitSerializer = serviceProvider.GetRequiredService<IMathUnitSerializerProvider>().GetSerializer("UCUM");
var ucumSymbolRegistry = serviceProvider.GetRequiredService<IMathUcumSymbolRegistry>();

// Register the UCUM symbols for the units.
ucumSymbolRegistry.RegisterUnit(units.Length.Meter, new MathUcumSymbol("m", "m", "M", name: "meter"));
ucumSymbolRegistry.RegisterUnit(units.Length.InchI, new MathUcumSymbol("[in_i]", "[in_i]", "[IN_I]", name: "inch"));

// Format meter as text.
var unitAsText = unitSerializer.Format(units.Length.Meter, out var remainderScale, culture: MathUcumUnitSerializer.CaseInsensitiveCulture);
Assert.AreEqual("M", unitAsText);
Assert.AreEqual(eMathScalePrefix.None, remainderScale);

// Format inch as text.
unitAsText = unitSerializer.Format(units.Length.InchI, out remainderScale, culture: MathUcumUnitSerializer.CaseInsensitiveCulture);
Assert.AreEqual("[IN_I]", unitAsText);
Assert.AreEqual(eMathScalePrefix.None, remainderScale);

// Parse the meter as text as unit.
using (unitSerializer.Parse<eMathScalePrefix, int>("m", out var scale, culture: MathUcumUnitSerializer.CaseInsensitiveCulture).Deconstruct(out var unit))
{
    Assert.AreEqual(units.Length.Meter, unit);
    Assert.AreEqual(eMathScalePrefix.None, scale);
}

// Parse the inch as text as unit.
using (unitSerializer.Parse<eMathScalePrefix, int>("[in_I]", out var scale, culture: MathUcumUnitSerializer.CaseInsensitiveCulture).Deconstruct(out var unit))
{
    Assert.AreEqual(units.Length.InchI, unit);
    Assert.AreEqual(eMathScalePrefix.None, scale);
}

Serializing UOMs using the UCUM standard

IServiceProvider serviceProvider = ...;

var units = serviceProvider.GetRequiredService<IMathUnits>().GetGeneric<eMathScalePrefix, int>();
var uomFactory = serviceProvider.GetRequiredService<IMathUomFactory>().GetGeneric<decimal, eMathScalePrefix, int>();
var uomSerializer = serviceProvider.GetRequiredService<IMathUomSerializerProvider>().GetSerializer("UCUM");
var ucumSymbolRegistry = serviceProvider.GetRequiredService<IMathUcumSymbolRegistry>();

// Register the UCUM symbols for the units.
ucumSymbolRegistry.RegisterUnit(units.Length.Meter, new MathUcumSymbol("m", "m", "M", name: "meter"));
ucumSymbolRegistry.RegisterUnit(units.Length.InchI, new MathUcumSymbol("[in_i]", "[in_i]", "[IN_I]", name: "inch"));

// Create a UOM in meter.
using (var uom = uomFactory.New(5, units.Length.Meter))
{
    // Format the UOM as text.
    var uomAsText = uomSerializer.Format(uom, culture: MathUcumUomSerializer.CaseInsensitiveCulture);
    Assert.AreEqual("5 M", uomAsText);
}

// Create a UOM in inch.
using (var uom = uomFactory.New(3, units.Length.InchI))
{
    // Format the UOM as text.
    var uomAsText = uomSerializer.Format(uom, culture: MathUcumUomSerializer.CaseInsensitiveCulture);
    Assert.AreEqual("3 [IN_I]", uomAsText);
}

// Parse a text as UOM.
using (var uom = uomSerializer.Parse<decimal, eMathScalePrefix, int>("5 m", culture: MathUcumUomSerializer.CaseInsensitiveCulture))
{
    Assert.AreEqual(5m, uom.Quantity);
    Assert.AreEqual(units.Length.Meter, uom.Unit);
}

// Parse a text as UOM.
using (var uom = uomSerializer.Parse<decimal, eMathScalePrefix, int>("3 [IN_i]", culture: MathUcumUomSerializer.CaseInsensitiveCulture))
{
    Assert.AreEqual(3m, uom.Quantity);
    Assert.AreEqual(units.Length.InchI, uom.Unit);
}

Have a look at the example applications on equadrat.net and in the wiki.

Compatibility

The plan is to keep the equadrat Framework compatible to these .net versions as long as possible.

.net version min max automated tests*
Core 3.1 ≥ 10.x yes
Standard 2.0 ≥ 2.1 no
Framework 4.6.2 ≥ 4.8.x no

*The automated tests run under Linux for .net Core 6.x and above.

Version scheme and breaking changes

Version change Scope
Major Overhaul of the project
Minor Some types have changed
Revision Fully backward compatible
Build Set in preview versions only

Additional documentation

You can find some guides regarding specific features on my website: www.equadrat.net

Feedback

You can contact me on my website: www.equadrat.net

License

Please respect the license and check equadrat.Framework.Math.License.md before using the package.

Release notes

v8.3.6

  • Adjusted code to work with limitations of Microsoft Dependency Injection.
  • Adjusted behaviors of UOM operators when one or both members are empty.

v8.3.5

  • Initial Release

Dependencies

ID Version Target Framework
equadrat.Framework.Math.Interfaces 8.3.6 .NETStandard2.0
equadrat.Framework.Math 8.3.6 .NETStandard2.0
equadrat.Framework.Math.Uom.Interfaces 8.3.6 .NETStandard2.0
equadrat.Framework.Math.Uom.Ucum.Interfaces 8.3.6 .NETStandard2.0
equadrat.Framework.Math.Uom 8.3.6 .NETStandard2.0
Microsoft.Bcl.AsyncInterfaces 10.0.11 .NETStandard2.0
equadrat.Framework.Core 8.3.5 .NETStandard2.0
equadrat.Framework.Core.Interfaces 8.3.5 .NETStandard2.0
equadrat.Framework.Math.Interfaces 8.3.6 .NETStandard2.1
equadrat.Framework.Math 8.3.6 .NETStandard2.1
equadrat.Framework.Math.Uom.Interfaces 8.3.6 .NETStandard2.1
equadrat.Framework.Math.Uom.Ucum.Interfaces 8.3.6 .NETStandard2.1
equadrat.Framework.Math.Uom 8.3.6 .NETStandard2.1
equadrat.Framework.Core 8.3.5 .NETStandard2.1
equadrat.Framework.Core.Interfaces 8.3.5 .NETStandard2.1
equadrat.Framework.Math.Interfaces 8.3.6 net10.0
equadrat.Framework.Math 8.3.6 net10.0
equadrat.Framework.Math.Uom.Interfaces 8.3.6 net10.0
equadrat.Framework.Math.Uom.Ucum.Interfaces 8.3.6 net10.0
equadrat.Framework.Math.Uom 8.3.6 net10.0
equadrat.Framework.Core 8.3.5 net10.0
equadrat.Framework.Core.Interfaces 8.3.5 net10.0
equadrat.Framework.Math.Interfaces 8.3.6 net5.0
equadrat.Framework.Math 8.3.6 net5.0
equadrat.Framework.Math.Uom.Interfaces 8.3.6 net5.0
equadrat.Framework.Math.Uom.Ucum.Interfaces 8.3.6 net5.0
equadrat.Framework.Math.Uom 8.3.6 net5.0
equadrat.Framework.Core 8.3.5 net5.0
equadrat.Framework.Core.Interfaces 8.3.5 net5.0
equadrat.Framework.Math.Interfaces 8.3.6 net6.0
equadrat.Framework.Math 8.3.6 net6.0
equadrat.Framework.Math.Uom.Interfaces 8.3.6 net6.0
equadrat.Framework.Math.Uom.Ucum.Interfaces 8.3.6 net6.0
equadrat.Framework.Math.Uom 8.3.6 net6.0
equadrat.Framework.Core 8.3.5 net6.0
equadrat.Framework.Core.Interfaces 8.3.5 net6.0
equadrat.Framework.Math.Interfaces 8.3.6 net7.0
equadrat.Framework.Math 8.3.6 net7.0
equadrat.Framework.Math.Uom.Interfaces 8.3.6 net7.0
equadrat.Framework.Math.Uom.Ucum.Interfaces 8.3.6 net7.0
equadrat.Framework.Math.Uom 8.3.6 net7.0
equadrat.Framework.Core 8.3.5 net7.0
equadrat.Framework.Core.Interfaces 8.3.5 net7.0
equadrat.Framework.Math.Interfaces 8.3.6 net8.0
equadrat.Framework.Math 8.3.6 net8.0
equadrat.Framework.Math.Uom.Interfaces 8.3.6 net8.0
equadrat.Framework.Math.Uom.Ucum.Interfaces 8.3.6 net8.0
equadrat.Framework.Math.Uom 8.3.6 net8.0
equadrat.Framework.Core 8.3.5 net8.0
equadrat.Framework.Core.Interfaces 8.3.5 net8.0
equadrat.Framework.Math.Interfaces 8.3.6 net9.0
equadrat.Framework.Math 8.3.6 net9.0
equadrat.Framework.Math.Uom.Interfaces 8.3.6 net9.0
equadrat.Framework.Math.Uom.Ucum.Interfaces 8.3.6 net9.0
equadrat.Framework.Math.Uom 8.3.6 net9.0
equadrat.Framework.Core 8.3.5 net9.0
equadrat.Framework.Core.Interfaces 8.3.5 net9.0