Home > Web Front-end > JS Tutorial > Quick Tip: How to Convert Numbers to Ordinals in JavaScript

Quick Tip: How to Convert Numbers to Ordinals in JavaScript

尊渡假赌尊渡假赌尊渡假赌
Release: 2025-02-09 12:48:15
Original
1001 people have browsed it

Quick Tip: How to Convert Numbers to Ordinals in JavaScript

This tutorial will guide you how to convert numbers to ordinal numbers in JavaScript. Getting the ordinal number of numbers allows you to display it in a human-readable format.

Key Points

  • Ordinal defines the position of numbers in a sequence or sequence, and can be used to make numerical data in JavaScript easier to read by humans, such as displaying dates or rankings.
  • In English, the ordinal suffix rule is very simple: "st" is appended to a number that is one and one more than a multiple of ten (excluding multiples of 11 and 100 plus 11), and "nd" is appended to 2 and more than ten The number of multiples sophomore (excluding multiples of 12 and 100 plus 12), "rd" is attached to the number of multiples of 3 and the number of multiples of 10 (excluding multiples of 13 and 100 plus 13), "th" is attached to all Other numbers.
  • You can use the JavaScript function to convert the number to its ordinal form by setting the default ordinal value to "th", and then using the remainder (%) operator to check whether the number meets various conditions (if necessary).

What is an ordinal number?

Ordinal numbers define numbers as part of order or sequence. "First", "Second" and "Third" are all examples of ordinal numbers. When using numbers to display chart results, month dates, or rankings, you usually need to use ordinal numbers.

Numbers can be used to display many different types of data and results. When presenting numbers to users, they usually need to be rendered in a more readable format—such as adding an ordinal suffix (e.g., "June 12" instead of "June 12").

Ordinal suffix rules in English

Let's see how ordinal numbers are used in English. English ordinal numbers follow a predictable, if not very simple rules:

  • "st" is appended to a number that is one and one more than the multiple of ten, except for 11 and one more than the multiple of 100. For example, 1st, 21st, 31st, etc...but 11th, 111th, etc.
  • "nd" is appended to the number that is 2 and a multiple of ten, except for the number that is 12 and 12 larger than the multiple of 100. For example, 2nd, 22nd, 32nd, etc....but 12th, 112th, etc.
  • "rd" is attached to the number that is larger than the multiple of ten, except for the number that is larger than the multiple of 13 and 13 than the multiple of 100. For example, 3rd, 23rd, 33rd, etc....but 13th, 113th, etc.
  • "th"attach to all other numbers. For example, 24th.

How to get the ordinal number of numbers?

To get the ordinal 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;
}
Copy after login

getOrdinal The function takes a number as an argument and returns the ordinal number of that number. Since most ordinal numbers end with "th", the default value for ord is set to th. You then test the numbers based on different conditions and change the ordinal number if necessary.

You will notice that the remainder (%) operator is used in each condition. This operator returns the remaining value after dividing the left operand by the right operand. For example, 112 % 100 returns 12.

To test if a number should have an ordinal st, you need to check if n is one larger than a multiple of ten (n % 10 == 1, including 1 itself), but not 11 (n % 100 != 11, including 11 itself).

To test if a number should have an ordinal nd, you need to check if n is older than a multiple of ten (n % 10 == 2, including 2 itself), but not 12 (n % 100 != 12, including 12 itself).

To test if a number should have an ordinal rd, you need to check if n is three times larger than a multiple of ten (n % 10 == 3, including 3 itself), but not 13 times larger than a multiple of 100 (n % 100 != 13, including 13 itself).

If all conditions are false, the value of ord remains at th.

Conclusion

In this tutorial, you learned how to retrieve ordinal numbers of numbers. Ordinal numbers can be used in a variety of situations, such as displaying dates or rankings in a human-readable format.

(Subsequent content, such as the FAQ section, can be rewrite similarly based on the original text, maintaining the consistency of the content, and adjusting the sentences and expressions to make them more natural and smooth.)

The above is the detailed content of Quick Tip: How to Convert Numbers to Ordinals in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template