## Quick Tip: How to Convert Numbers to Ordinals in JavaScript

In this tutorial, you will learn how to convert numbers to ordinals in JavaScript. Getting the serial number of a number allows you to display it in a human-readable format.

## What are ordinals?

Ordinals define numbers as being part of an order or sequence. The words “first”, “second”, and “third” are all examples of ordinals. When using numbers to display chart results, days of the month, or rankings, you’ll often need to use ordinals.

Numbers can be used to display many different types of data and results. When numbers are presented to users, they often need to be presented in a format that is more readable—such as adding ordinal suffixes (“June 12th” instead of “June 12th”, for example).

## ordinal suffix rules in english

Let us see how ordinals are used in the English language. The English Ordinances follow a predictable, if not pretty simple, set of rules:

• “Cent” is combined with 1 and numbers that are one greater than a multiple of ten, except 11, and numbers that are 11 more than a multiple of 100. For example, 1st, 21st, 31st, etc… but 11th, 111th, etc.

• “ND” is added to 2 more numbers that are two more than a multiple of ten, except 12 and numbers that are 12 more than a multiple of 100. For example, 2nd, 22nd, 32nd, etc… but 12th, 112th, etc.

• “Third” is added to 3 and numbers that are multiples of ten greater than three except 13 and numbers that are multiples of 100 greater than 13. For example, 3rd, 23rd, 33rd, etc… but 13th, 113th, etc.

• “Th” is added to everything else. For example, 24.

## how to find sequence of a number

To get the serial number of a number, you can use the following function:

function getOrdinal(n)
let ord = 'th';

if (n % 10 == 1 && n % 100 != 11)

ord = 'st';

else if (n % 10 == 2 && n % 100 != 12)

ord = 'nd';

else if (n % 10 == 3 && n % 100 != 13)

ord = 'rd';

return ord;

Program getOrdinal Accepts one argument that is a number and returns the ordinal of that number. Since most ordinals end in “th”, the default value of ord it’s on th, Then, you test the number at different positions and change the ordinal if necessary.

You will find that in each case balance (%) operator Is used for. This operator returns the remainder of dividing the left operand by the right operand. For example, 112 % 100 Return 12,

How to check if a number should have an ordinal stif you check n is greater than a multiple of ten (n % 10 == 1which contains 1 itself), but 11 is not a multiple of 100 (n % 100 != 11Including 11 only).

How to check if a number should have an ordinal ndif you check n 2 is greater than a multiple of ten (n % 10 == 2 which itself contains 2), but 12 is not greater than a multiple of 100 (n % 100 != 12which contains only 12).

How to check if a number should have an ordinal rdif you check n 3 is greater than a multiple of ten (n % 10 == 3which itself contains 3), but 13 is not greater than a multiple of 100 (n % 100 != 13which contains only 13).

If all conditions are false, then the value of ord Ruins th,

You can test it out in live action with the following Codepen demo.

see pen
get sequence of a number
by sitepoint (@SitePoint,
Feather codepen,

## conclusion

In this tutorial, you learned how to find the ordinal of a number. Ordinals can be used in a variety of cases, such as displaying dates or rankings in human-readable formats.

If you found this article useful, you may also enjoy the following: