angularsen / UnitsNet

Makes life working with units of measurement just a little bit better.
https://www.nuget.org/packages/UnitsNet/
MIT No Attribution
2.2k stars 380 forks source link

Add API to for From UnitSystem #1271

Closed holmes-mike closed 3 months ago

holmes-mike commented 1 year ago

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);
angularsen commented 1 year ago

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.

github-actions[bot] commented 3 months ago

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] commented 3 months ago

This issue was automatically closed due to inactivity.