formatUnitAbbr()function

formatUnitAbbr(unit: string, options?: UnitFormatOptions): string
ParamType
unitstring
Unit reference, e.g. "minute" or "product". required
optionsUnitFormatOptions
Options for quantity formatting.
    .onestring
String for one of this thing, e.g. product or item or sheep
- Used for unitDisplay: "long" formatting.
- Defaults to unit reference, e.g. "minute" readonly
    .manystring
String for several of this thing, e.g. products or items or sheep
- Used for unitDisplay: "long" formatting.
- Defaults to one + "s" readonly
    .abbrstring
Abbreviation for this thing, e.g. products or items or sheep (defaults to one + "s").
- Used for unitDisplay: "narrow" formatting.
- Defaults to unit reference, e.g. "minute" readonly
Return
string
The short name of the unit, e.g. "min".

Format the short name of a unit on its own, e.g. "km" or "min", with no number.

  • Uses the Intl.NumberFormat short name when the browser supports the unit, so the name is translated (e.g. "Std." for hour in German).
  • Falls back to options.abbr, then to the unit reference itself, when the browser does not support the unit.
  • Gives the name for an amount of one, so some units in some locales differ from their plural form (e.g. "day" not "days").

Examples

formatUnitAbbr("kilometer") // "km"
formatUnitAbbr("dog", { abbr: "🐶" }) // "🐶"