Answers for "int to ordinals typescript"

1

int to ordinals typescript

/**
 * intToOrdinalNumberString converts an integer to an ordinal
 * number string. For example: 1 => "1st", 12 => "12th", 23 => "23rd"
 */
intToOrdinalNumberString (num: number): string {
	num = Math.round(num);
	let numString = num.toString();

	// If the ten's place is 1, the suffix is always "th"
	// (10th, 11th, 12th, 13th, 14th, 111th, 112th, etc.)
	if (Math.floor(num / 10) % 10 === 1) {
		return numString + "th";
	}

	// Otherwise, the suffix depends on the one's place as follows
	// (1st, 2nd, 3rd, 4th, 21st, 22nd, etc.)
	switch (num % 10) {
		case 1: return numString + "st";
		case 2: return numString + "nd";
		case 3: return numString + "rd";
		default: return numString + "th";
	}
}
Posted by: Guest on June-30-2021

Code answers related to "TypeScript"

Browse Popular Code Answers by Language