Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #370 Determine the Numeral value of an Integer

Create the end-user text, s, specifying the numeral value of integer a.

For example, 1 is "1st", 2 is "2nd", 3 is "3rd", etc.

#include <iostream>
using namespace std;
string f(int& a) {
    string s;
    int x { a % 100 };
    if (x > 9 && x < 14) s = "th";
    else switch (a % 10) {
        case 1:
            s = "st";
            break;
        case 2:
            s = "nd";
            break;
        case 3:
            s = "rd";
            break;
        default:
            s = "th";
    }
    return to_string(a) + s;
}
String s;
int x = a % 100;
if (x > 9 && x < 14) s = "th";
else s = switch (a % 10) {
    case 1 -> "st";
    case 2 -> "nd";
    case 3 -> "rd";
    default -> "th";
};
s = a + s;
  if ((a mod 100) in [10..20]) then
    s := 'th'
  else
  case (a mod 10) of
    1: s := 'st';
    2: s := 'nd';
    3: s := 'rd';
    otherwise
      s := 'th';
  end;
  s := a.ToString + s;
if 9 < (a % 100) < 14: s = 'th'
else:
    match a % 10:
        case 1: s = 'st'
        case 2: s = 'nd'
        case 3: s = 'rd'
        case _: s = 'th'
s = str(a) + s

New implementation...
< >
reilas