Convert Number into Ordinal

Convert Number into Ordinal

Hello Developers,

Here is the solution for converting the Number into Ordinal.

For Example:

This Idea can be used when lets say you are creating the Dashboard  where you are showing upcoming celebrations like work Anniversary for you HRMS Application.

Once employee completes 1 year then it will show 1st Anniversary and say if completes 2 years the it will show 2nd Anniversary.

To Achieve this just create a custom function which take input as Number and Return String please find the deluge script for the same below.

  1. string getOrdinal(int n)
  2. {
  3. ord = "th";
  4.   if (n % 10 == 1 && n % 100 != 11)
  5.   {
  6. ord = "st";
  7.   }
  8.   else if (n % 10 == 2 && n % 100 != 12)
  9.   {
  10. ord = "nd";
  11.   }
  12.   else if (n % 10 == 3 && n % 100 != 13)
  13.   {
  14. ord = "rd";
  15.   }

  16.   return n + ord;
  17. }
  18.  

Hope this helps!

Regards, 
Anurag Jaiswal