UnitsNet icon indicating copy to clipboard operation
UnitsNet copied to clipboard

Add API to for From UnitSystem

Open holmes-mike opened this issue 1 year ago • 1 comments

I am looking for an API to convert from one unit system to another. I expect I have not found the correct API?

What I would expect to see is:

var value = 1.0;
var inputSystem = new UnitSystem(new BaseUnits(....));
var siValue = Length.From(value, inputSystem).As(UnitSystem.SI);

I'm not entirely sure what the purpose of the nullable unit types for a given system are. My current code looks like:

var value = 1.0;
var inputSystem = new UnitSystem(new BaseUnits(....));
va siValue = Length.From(value, inputSystem.BaseUnits.Length!.Value).As(UnitSystem.SI);

however this does not extend so well to things like Pressure.

var value = 1.0;
var inputSystem = new UnitSystem(new BaseUnits(....));
va siValue = Pressure.From(value, ???).As(UnitSystem.SI);

holmes-mike avatar Jun 22 '23 02:06 holmes-mike

You can do var myPressure = new Pressure(5, myUnitSystem) to create 5 Pascal if given UnitSystem.SI, and you can use myPressure.As(myUnitSystem) to get the numeric value for your configured unit system.

If you create a unit system where L=Centimeter, then Area and Volume would be displayed or converted to centimeters instead of meters.

However, UnitSystem is not fully supported yet and has some shortcomings.

Prefix units are missing base units (e.g. Kilogram), to be fixed by #1233 It may not be able to find a unit for quantities not based on length, such as Angle. It may not be able to find a unique unit for all quantities were multiple units share the same SI base units.

Some more details here: https://github.com/angularsen/UnitsNet/pull/1233#issuecomment-1573678059

Personally, if I were to offer unit preferences in an application, I would probably create a custom mapping between SI base units and units in the quantities I care about.

angularsen avatar Jul 10 '23 20:07 angularsen

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Jul 08 '24 18:07 github-actions[bot]

This issue was automatically closed due to inactivity.

github-actions[bot] avatar Jul 16 '24 02:07 github-actions[bot]