Skip to content

Core\Cldr::ordinalCategory

Tells you which form a number takes when it is a place rather than an amount, such as English's 1st, 2nd, 3rd and 4th.

ordinalCategory returns the form for the place you give it: One, Two, Few, Many, Zero or Other. English uses four of them, so a template picks st, nd, rd or th from the answer. Most languages mark no place at all and return Other for every number. German writes 3. for every place, and Japanese writes the number alone.

The second argument is a language tag such as "en", "it" or "cy". It is read the same way Core\Cldr::pluralCategory reads it: only the language part, and upper and lower case are the same. A language Novis carries no rules for throws a LogicError.

Good to know: the form for a place is not the form for an amount. In English, 3 books is Other and 3rd place is Few. Use this member for places and Core\Cldr::pluralCategory for amounts.

The examples below show the English suffixes, then how the two members differ for the same number, then a results list written in two languages.

Parameters

  • $countint|float|decimalThe position the message is about. The CLDR rules for positions are for whole numbers, so a count with a fraction gives Other. Macedonian and Georgian are different, because their rules read only the whole-number part.
  • $localestringA BCP 47 tag, read the same way as in pluralCategory. Only the language part is read, and case is ignored.

Return value

Returns Core\Cldr::PluralCategory. The category of $count as a position in this language. It is Other for every count in a language with no special forms for positions, which is most languages.

Errors

  • LogicErrorThe tag has no language part, or Novis has no plural rules and no position rules for its language. A language with plural rules but no position forms does not throw. It returns Other.
  • RuntimeError$count is a float that is not finite. It is also thrown for a float that prints more digits than the rules can check.

Examples

English place suffixes

<?nvs
// Shows how to write the English suffix for a place: 1st, 2nd, 3rd, 4th.
class Place
{
public static function of(int $number): string
{
// `Core\Cldr::ordinalCategory` returns the form English uses for this
// place. Each of the three marked forms has its own suffix.
string $suffix = match (Core\Cldr::ordinalCategory($number, 'en')) {
Core\Cldr::PluralCategory::One => 'st',
Core\Cldr::PluralCategory::Two => 'nd',
Core\Cldr::PluralCategory::Few => 'rd',
default => 'th',
};
return ($number as string) . $suffix;
}
}
// This prints 1st, 2nd, 3rd, 4th, 11th, 12th, 13th, 21st and 22nd. The rules
// know that 11, 12 and 13 take "th" even though 1, 2 and 3 do not.
array<int> $places = [1, 2, 3, 4, 11, 12, 13, 21, 22];
foreach ($places as int $number) {
echo Place::of($number), "\n";
}
Output
1st
2nd
3rd
4th
11th
12th
13th
21st
22nd

A place is not an amount

<?nvs
// Shows that a number as a place is not the same as a number as an amount.
// The two members answer differently for the same number.
class Form
{
public static function name(Core\Cldr::PluralCategory $form): string
{
return match ($form) {
Core\Cldr::PluralCategory::Zero => 'zero',
Core\Cldr::PluralCategory::One => 'one',
Core\Cldr::PluralCategory::Two => 'two',
Core\Cldr::PluralCategory::Few => 'few',
Core\Cldr::PluralCategory::Many => 'many',
Core\Cldr::PluralCategory::Other => 'other',
};
}
}
// In English, 3 books is `other` and 3rd place is `few`. A page that keys its
// places on the amount form writes "3th".
array<int> $numbers = [1, 2, 3, 4];
foreach ($numbers as int $number) {
echo $number,
' amount=', Form::name(Core\Cldr::pluralCategory($number, 'en')),
' place=', Form::name(Core\Cldr::ordinalCategory($number, 'en')),
"\n";
}
Output
1 amount=one place=one
2 amount=other place=two
3 amount=other place=few
4 amount=other place=other

A results list in two languages

<?nvs
// Shows a results list written in two languages. A template does this when the
// same page is shown to readers who do not share a language.
class Place
{
public static function write(int $number, string $locale, array<string> $suffixes): string
{
string $form = match (Core\Cldr::ordinalCategory($number, $locale)) {
Core\Cldr::PluralCategory::One => 'one',
Core\Cldr::PluralCategory::Two => 'two',
Core\Cldr::PluralCategory::Few => 'few',
default => 'other',
};
// A language that marks no place has only the `other` suffix, so that
// is the one to use when the form is missing.
if (!Core\Arr::hasKey($suffixes, $form)) {
$form = 'other';
}
return ($number as string) . $suffixes[$form];
}
}
// English writes a different suffix for three of the forms. German writes a
// full stop after every place.
array<string> $english = ['one' => 'st', 'two' => 'nd', 'few' => 'rd', 'other' => 'th'];
array<string> $german = ['other' => '.'];
array<string> $finishers = ['Ada', 'Grace', 'Alan', 'Edsger'];
int $place = 1;
foreach ($finishers as string $name) {
echo $name, ': ', Place::write($place, 'en', $english),
' / ', Place::write($place, 'de', $german), "\n";
$place = $place + 1;
}
Output
Ada: 1st / 1.
Grace: 2nd / 2.
Alan: 3rd / 3.
Edsger: 4th / 4.