toOrdinal method

String toOrdinal({
  1. bool asWord = false,
  2. bool includeAnd = false,
})

Converts the number to its ordinal representation as either a number with a suffix (e.g., "1st") or as a word (e.g., "first").

Implementation

String toOrdinal({bool asWord = false, bool includeAnd = false}) {
  final number = toInt();

  // Return 'zeroth' or '0th' based on asWord
  if (number == 0) return asWord ? 'zeroth' : '0th';

  // Handling negative numbers
  if (number < 0) throw ArgumentError('Negative numbers are not supported');

  return asWord
      ? _dynamicOrdinalWord(number, includeAnd: includeAnd)
      : _asOrdinalSuffixWithNumber();
}