int to ordinals typescript

Solutions on MaxInterview for int to ordinals typescript by the best coders in the world

showing results for - "int to ordinals typescript"
Adriana
23 Aug 2020
1/**
2 * intToOrdinalNumberString converts an integer to an ordinal
3 * number string. For example: 1 => "1st", 12 => "12th", 23 => "23rd"
4 */
5intToOrdinalNumberString (num: number): string {
6	num = Math.round(num);
7	let numString = num.toString();
8
9	// If the ten's place is 1, the suffix is always "th"
10	// (10th, 11th, 12th, 13th, 14th, 111th, 112th, etc.)
11	if (Math.floor(num / 10) % 10 === 1) {
12		return numString + "th";
13	}
14
15	// Otherwise, the suffix depends on the one's place as follows
16	// (1st, 2nd, 3rd, 4th, 21st, 22nd, etc.)
17	switch (num % 10) {
18		case 1: return numString + "st";
19		case 2: return numString + "nd";
20		case 3: return numString + "rd";
21		default: return numString + "th";
22	}
23}