suffix method

String suffix([
  1. String suffix = ''
])

Adds a suffix to the number.

This method takes an optional parameter suffix, which is a string that will be appended to the number. If suffix is not provided, it defaults to an ordinal suffix (e.g. 1st, 2nd, 3rd, etc.).

Example usage:

  print(5.suffix()); // prints "5th"
  print(5.suffix('%')); // prints "5%"

Implementation

String suffix([String suffix = '']) {
  if (suffix.isNotEmpty) {
    return '$this$suffix';
  }
  switch (this % 100) {
    case 11:
    case 12:
    case 13:
      return '${this}th';
    default:
      switch (this % 10) {
        case 1:
          return '${this}st';
        case 2:
          return '${this}nd';
        case 3:
          return '${this}rd';
        default:
          return '${this}th';
      }
  }
}