Skip to content

Design Proposal: unit option for number-format #1336

@ginnyTheCat

Description

@ginnyTheCat

Design Proposal: unit option for number-format

Motivation

Allowing one to format heights of mountains (or other units of distance, time, weight, etc.) which are often to found on maps. Since a option to format currencies already exists adding one for other units would be nice.

For example using ["number-format", ["get", "elevation", { locale: <language>, unit: "meter" }] to display 5.000 m in de-DE, 5,000米 in cmn-CN and 5 000 м in uk-UA.

Proposed Change

Add a new unit option to number-format that is mutually exclusive to the currency option that already exists for monetary quantities. The possible units allowed are specified in CLDR and ECMA-402.

In JavaScript this option when specified sets the Intl.NumberFormat style to "unit" and passes the unit via the unit option. Other platforms are likely to have similar functionality, since this is something ICU offers. A prototype implementation can be found in #1335.

API Modifications

A new unit option in the number-format options.

Migration Plan and Compatibility

This feature only adds new functionality and is fully backwards compatible.

Rejected Alternatives

I have looked for other issues and not found any about this.

I tried implementing this myself by formatting a arbitrary number using Intl.NumberFormat.formatToParts, finding the number using the type and then concatenating the rest on the start and end of a number expression. This however does not work since some languages change the unit display based on the number provided (pluralization rules). Also this is not doable when specifying styles using JSON and not during runtime using JavaScript.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions