toOrdinal method

String toOrdinal([
  1. String space = ''
])

Converts an int into an ordinal number as a String.

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';
  }
}