Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API to for From UnitSystem #1271

Open
holmes-mike opened this issue Jun 22, 2023 · 1 comment
Open

Add API to for From UnitSystem #1271

holmes-mike opened this issue Jun 22, 2023 · 1 comment

Comments

@holmes-mike
Copy link

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
Copy link
Owner

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:
#1233 (comment)

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants