• Region-aware unit conversion utility that can be used to convert between units of measure.

    When converting between units of measure, the source and target unit can be specified as a string or as a {@see UnitSource} object.

    Parameters

    • value: number

      the value to convert

    • from: UnitConversionSource

      the source unit

    • Optional to: UnitConversionSource

      the target unit. If omitted, the value will be converted to the default unit for the region measure.

    Returns string

    the converted value

    Example

    in the simplest case, the source and target units can be specified as strings so long as they belong to the same measure (volume, length etc):

    import { convert } from "@kosdev-code/kos-ui-sdk";

    const value = convert(1000, "milliliter", "fluid-ounce");

    The to unit can be omitted in which case the value will be converted to the default unit for the selected region measure:

    Example

    import { convert } from "@kosdev-code/kos-ui-sdk";

    // convert 1000 milliliters to the default unit for the region. If the region is set to US, the value will be converted to fluid ounces.
    const value = convert(1000, "milliliter");

    The source and target units can also be specified as {@see UnitSource} objects:

    Example

    import { convert } from "@kosdev-code/kos-ui-sdk";

    const value = convert(1000, {name: "milliliter", measure: "volume", system: "si"}, {name: "fluid-ounce", measure: "volume", system: "us"});

    In this case properties on the source and target are all optional so long as either the name or measure is specified.