• Instructions to Ask a Question

    Click on the "Ask a Question" button and select the application for which you would like to ask questions.

    We have 5 different products namely - Pabbly Connect, Pabbly Subscription Billing, Pabbly Email Marketing, Pabbly Form Builder, Pabbly Email Verification.

    The turnaround time is 24 hrs (Business Hours - 10.00 AM to 6.00 PM IST, Except Saturday and Sunday). So your kind patience will be highly appreciated!

    🚀🚀Exclusive Discount Offer

    Just in case you're looking for any ongoing offers on Pabbly, you can check the one-time offers listed below. You just need to pay once and use the application forever -
     

    🔥 Pabbly Connect One Time Plan for $249 (🏆Lifetime Access) -  View offer 

    🔥 Pabbly Subscription Billing One Time Plan for $249 (🏆Lifetime Access) - View offer

convert numbers to ordinal numbers

Stuart

Member
Hi,
Is there any way to convert numbers to ordinal form?
That is 1 > 1st, 2 > 2nd 3 > 3rd, 123 > 123rd.
I could do it with a lookup table for 1 and 2 but that's not a free task!
 

Fagun Shah

Well-known member
Try this JS code action instead, which converts multiple comma separated cardinal numbers to ordinal numbers -



function convertToOrdinal(numbers) {
if (!Array.isArray(numbers)) {
return ['Input should be an array of numbers'];
}

const ordinalForms = [];

for (const number of numbers) {
if (typeof number !== 'number') {
ordinalForms.push('Not a number');
} else if (number % 1 !== 0) {
ordinalForms.push('Not an integer');
} else if (number >= 11 && number <= 13) {
ordinalForms.push(number + 'th');
} else {
switch (number % 10) {
case 1:
ordinalForms.push(number + 'st');
break;
case 2:
ordinalForms.push(number + 'nd');
break;
case 3:
ordinalForms.push(number + 'rd');
break;
default:
ordinalForms.push(number + 'th');
}
}
}

return ordinalForms;
}

// Example usage:
const result = convertToOrdinal([12, 1, 13]);//Replace 12,1,13 with your numbers
return (result);


1698807448540.png
 
Top