ordinal property

String ordinal

Converts an int into its English ordinal representation.

Example:

print(1.ordinal);   // Output: 1st
print(22.ordinal);  // Output: 22nd
print(143.ordinal); // Output: 143rd
print(0.ordinal);   // Output: 0th
print(12.ordinal);  // Output: 12th
print(69.ordinal);  // Output: 69th

The ordinal extension converts an integer into its English ordinal representation. It is commonly used to format numbers as ordinals, such as "1st", "22nd", "143rd", etc.

Implementation

String get ordinal {
  return switch (toInt() % 100) {
    11 || 12 || 13 => '${this}th',
    _ => switch (this % 10) {
        1 => '${this}st',
        2 => '${this}nd',
        3 => '${this}rd',
        _ => '${this}th'
      }
  };
}