Logo

Programming-Idioms

  • Elixir

Idiom #370 Ordinal form

Assign to the string s the ordinal number word for the integer a.

1 → "1st", 2 → "2nd", 3 → "3rd", etc.

String s;
int x = a % 100;
if (x > 9 && x < 21) s = "th";
else s = switch (a % 10) {
    case 1 -> "st";
    case 2 -> "nd";
    case 3 -> "rd";
    default -> "th";
};
s = a + s;

New implementation...
< >
reilas