fun Int.toOrdinalNumber(): String {
if (this in 11..13) {
return "${this}th"
}
return when (this % 10) {
1 -> "${this}st"
2 -> "${this}nd"
3 -> "${this}rd"
else -> "${this}th"
}
}
In this code, the In this code, the getOrdinalNumber extension function is added to the Int class. It first checks if the number is in the range of 11 to 13 because in these cases, the ordinal is always "th." For other cases, it checks the last digit of the number and appends "st," "nd," or "rd" accordingly. If none of these conditions match, it appends "th." extension function is added to the Int class. It first checks if the number is in the range of 11 to 13 because in these cases, the ordinal is always "th." For other cases, it checks the last digit of the number and appends "st," "nd," or "rd" accordingly. If none of these conditions match, it appends "th."