Skip to content

Novis\Intl\PluralRules::ordinal

Gives the category of each number as a place, such as first, second or third.

PluralRules::ordinal returns one Core\Cldr::PluralCategory for each number, in the same order as the list. The category tells you which ending goes with the number. In English, 1 is One for "1st", 2 is Two for "2nd", 3 is Few for "3rd", and 4 is Other for "4th". 11 is Other, for "11th", and 22 is Two, for "22nd". The locale you pass decides the rules.

PluralRules::ordinal throws a RuntimeError when a float is not finite. It throws a LogicError when the locale tag is not valid.

Good to know: the category is not the text. You choose the ending for each category yourself, and each language has its own endings. To count things, as in "2 files", use PluralRules::cardinal.

Parameters

  • $numbersarray<int|float|decimal>
  • $localestring

Return value

Returns array<Core\Cldr::PluralCategory>.

Examples

English endings

<?nvs
// Shows how to write the English ending of a place, such as "1st" or "22nd".
use Novis\Intl\PluralRules;
array<int|float|decimal> $places = [1, 2, 3, 4, 11, 22];
array<Core\Cldr::PluralCategory> $categories = PluralRules::ordinal($places, 'en');
// Each category has its own ending. This prints 1st 2nd 3rd 4th 11th 22nd.
array<string> $written = [];
for (int $i = 0; $i < 6; $i++) {
string $ending = match ($categories[$i]) {
Core\Cldr::PluralCategory::One => 'st',
Core\Cldr::PluralCategory::Two => 'nd',
Core\Cldr::PluralCategory::Few => 'rd',
default => 'th',
};
$written[] = "{$places[$i]}{$ending}";
}
echo Core\Str::join($written, ' '), "\n";
Output
1st 2nd 3rd 4th 11th 22nd

French endings

<?nvs
// Shows the endings of a place in French, which has two of them.
use Novis\Intl\PluralRules;
array<int|float|decimal> $places = [1, 2, 3];
array<Core\Cldr::PluralCategory> $categories = PluralRules::ordinal($places, 'fr');
// In French, only 1 is `One`. This prints 1er 2e 3e.
array<string> $written = [];
for (int $i = 0; $i < 3; $i++) {
string $ending = $categories[$i] == Core\Cldr::PluralCategory::One ? 'er' : 'e';
$written[] = "{$places[$i]}{$ending}";
}
echo Core\Str::join($written, ' '), "\n";
Output
1er 2e 3e

A place is not a count

<?nvs
// Shows that the same number can have one category as a count and another as a place.
use Novis\Intl\PluralRules;
array<int|float|decimal> $numbers = [2];
// As a count, 2 is `Other`, as in "2 files". This prints true.
echo PluralRules::cardinal($numbers, 'en')[0] == Core\Cldr::PluralCategory::Other ? 'true' : 'false', "\n";
// As a place, 2 is `Two`, as in "2nd". This prints true.
echo PluralRules::ordinal($numbers, 'en')[0] == Core\Cldr::PluralCategory::Two ? 'true' : 'false', "\n";
Output
true
true