Toolbox-XToolbox-X

Format Unit With Plural

Formats a grammatically correct unit string (using basic 's' suffix rules) based on a count.

formatUnitWithPlural

The formatUnitWithPlural function returns a basic unit string formatted for singular or plural forms (by appending 's' if the count is not 1). It can optionally prefix the unit with the number.

Tips

  • This function uses a simple approach of appending 's' to the unit.
  • For complex English grammar rules and irregular plurals (e.g. child ==> children), use Pluralizer Class instead.

Function Signature

function formatUnitWithPlural(count: number, unit: string, withNumber?: boolean): string

Parameters

  • count (number): The numeric value used to determine singular vs plural.
  • unit (string): The base unit name (e.g., "day", "hour").
  • withNumber (boolean, optional): Whether to prefix the result with the count. Defaults to true.

Return Value

  • string: The formatted string (e.g., "1 day" or "2 days" if withNumber is true, or "day" or "days" if false).

Example Usage

playground.ts

Notes

  • Absolute count is checked: any count other than 1 or -1 defaults to the plural form.

Aliases

The following aliases can be used for the formatUnitWithPlural function:

  • formatNumberWithPluralUnit
  • formatWithPlural

Last updated: Mon, Jun 15, 2026 11:04:47AM (UTC)

On this page