ordinal method

String ordinal()

Returns an ordinal number of String type for any integer

101.ordinal(); // 101st
114.ordinal(); // 101th

999218.ordinal(); // 999218th

A double typed version can use: value.floor()

Implementation

String ordinal() {
  // all "teens" (12, 13, 14.. 19) are 'th'
  if (this >= 11 && this <= 19) {
    return '${this}th';
  }

  final num onesPlace = this % 10;
  return switch (onesPlace) {
    1 => '${this}st',
    2 => '${this}nd',
    3 => '${this}rd',
    _ => '${this}th',
  };
}