Intl.NumberFormat
Stage: 1
Champion: Younies Mahmoud @younies
Author: Younies Mahmoud @younies
Representing measurements in localized units is a common need in internationalized software.
In the US and a handful of other countries, temperature is measured in degrees Fahrenheit. The rest of the world uses degrees Celsius. Some countries measure distances and lengths in miles, yards or feet, and inches, others use kilometers, meters, and centimeters. The Scandinavian Mile is a common unit of length in Norway and Sweden.
The measurement units used also depend on what exactly is being measured (roads, people, precipitation), not only on what kind of measurement it is (length, mass, pressure).
We propose adding locale- and usage-aware measurement formatting to ECMAScript's Intl.NumberFormat which will take care of such nuances.
Localized: for each locale, users should be presented with units appropriate for them. For example:
en-US
: milesfr-FR
: kilometersen-US
: poundsfr-FR
: kilogramsUsage: the preferred measurement unit is dependent on what is being measured. Consider:
en-US
: miles, feet, inchesfr-FR
: kilometers, meters, centimetersen-US
: inchesfr-FR
: millimetersThe choice of presentation units also depends on the size of the measurement:
en-US
, the length of toddlers is typically presented in inches,
whereas adults are measured feet and inches. (3 feet seems a reasonable
threshold.)
The precision of the displayed number also depends on usage:
en-US
, babies would be measured to a tenth of an inch, whereas
measurements presented in feet and inches are rounded to the nearest inch.
are measured to the nearest inch only.Add a usage
string parameter to Intl.NumberFormat
that indicates the
measurement use case.
person-height
, rainfall
, snowfall
, road
.A code example adding a usage
parameter to
Int.NumberFormat:
const inputValue = 1.8;
const inputUnit = "meter";
console.log(new Intl.NumberFormat('en-US', {
style: 'unit',
unit: inputUnit,
usage: 'person-height', // NEW parameter
unitDisplay: 'long'
}).format(inputValue));
// expected output: "5 feet and 11 inches"
// (1.8 meters = 5 feet 10.8661 inches")