const toOrdinalSuffix = (num: string) => {
        const int: number = parseInt(num),
          digits: number[] = [int % 10, int % 100],
          ordinals: string[] = ['st', 'nd', 'rd', 'th'],
          oPattern: number[] = [1, 2, 3, 4],
          tPattern: number[] = [11, 12, 13, 14, 15, 16, 17, 18, 19];
        return oPattern.includes(digits[0]) && !tPattern.includes(digits[1])
          ? int + ordinals[digits[0] - 1]
          : int + ordinals[3];
      };
      
      Typescript language logo

      Number to ordinal suffix

      Codiga's TypeScript Recipes

      Takes a number and returns it as a string with the correct ordinal indicator suffix.

      0 Comments

        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.