toOrdinal method
Converts an integer to an ordinal representation (e.g., 1st, 2nd, 3rd).
Example:
print(1.toOrdinal()); // 1st
print(22.toOrdinal()); // 22nd
Implementation
String toOrdinal([String space = '']) {
// Handle special case for numbers between 11 and 13, which all use 'th' suffix
if (this >= 11 && this <= 13) {
return '$this${space}th';
}
final onesPlace = this % 10;
switch (onesPlace) {
case 1:
return '$this${space}st';
case 2:
return '$this${space}nd';
case 3:
return '$this${space}rd';
default:
return '$this${space}th';
}
}