Skip to content

Improve unit conversion API to be more accessible outside of formatting #8877

@hl662

Description

@hl662

For users outside of the repo, calculating unit conversion between 2 units can be simplified.

Here is a code example of how users can do it now:

import { Quantity } from "@itwin/core-quantity";

const context = new SchemaContext(); // or from iModelDb.schemaContext

const provider = new SchemaUnitProvider(context);

const fromUnit = await provider.findUnitByName("Units.M");
const toUnit = await provider.findUnitByName("Units.FT");
const conversion = await provider.getConversion(fromUnit, toUnit);

const quantity = new Quantity(fromUnit, 1.0);
const converted = quantity.convertTo(toUnit, conversion);
console.log(converted?.magnitude);

It involves instantiating a Quantity object and calling the convertTo method, a wrapper around an internal applyConversion method in core-quantity. We should look into

  1. An easy way to convert values between units without needing an instanced object (which can mean making applyConversion not internal)
  2. Improving the documentation in quantity to explicitly have a code example for external developers to convert values between 2 units outside of formatting.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions