ordinal method
Returns an ordinal number of String type for any integer
101.ordinal(); // 101st
114.ordinal(); // 114th
999218.ordinal(); // 999218th
A double typed version can use: value.floor()
Audited: 2026-06-12 11:26 EDT
Implementation
@useResult
String ordinal() {
// 11th/12th/13th are 'th' — and so is ANY number ending in 11/12/13 (111th,
// 1012th, 213th). Test the last TWO digits, not an absolute 11..19 window:
// the old window left 111 → '111st'. Use abs() so a negative picks its suffix
// from the magnitude ((-21) → '-21st', not the '-21th' that `-21 % 10` gave).
final int magnitude = abs();
final int lastTwo = magnitude % _base100;
if (lastTwo >= _teenRangeStart && lastTwo <= _teenRangeEnd) {
return '${this}th';
}
return switch (magnitude % _base10) {
1 => '${this}st',
2 => '${this}nd',
_ordinalRdDigit => '${this}rd',
_ => '${this}th',
};
}