ordinalNumber method

String ordinalNumber(
  1. int n
)
inherited

Returns ordinal number string.

An example in English (US):

  • 1st
  • 12th
  • 2602nd
  • 303rd
  • 1000th

Implementation

String ordinalNumber(int n) {
  final int lastTwoDigit = n % 100;

  if (!(lastTwoDigit > 10 && lastTwoDigit < 14)) {
    final int lastDigit = n % 10;

    switch (lastDigit) {
      case 1:
        return "$n${ordinalSuffixes[1]}";
      case 2:
        return "$n${ordinalSuffixes[2]}";
      case 3:
        return "$n${ordinalSuffixes[3]}";
      default:
        break;
    }
  }

  return "$n${ordinalSuffixes[0]}";
}