Tuesday, February 7, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home JavaScript

Quick Tip: How to Convert Numbers to Ordinals in JavaScript

learningcode_x1mckf by learningcode_x1mckf
September 8, 2022
in JavaScript
0
Quick Tip: How to Convert Numbers to Ordinals in JavaScript
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


You might also like

An Introduction to Lodash and Its Benefits for JavaScript Developers – MUO – MakeUseOf

Mimic Javascript actions on identical element? – JavaScript – SitePoint

How To Hire a Professional JavaScript Developer and Find the Best … – Intelligent Living

On this tutorial, you’ll discover ways to convert numbers to ordinals in JavaScript. Getting the ordinal of a quantity lets you show it in a human-readable format.

What Are Ordinals?

Ordinals outline numbers as being a part of an order or sequence. The phrases “first”, “second”, and “third” are all examples of ordinals. When utilizing numbers to show chart outcomes, days of the month, or a rating, you’ll usually want to make use of ordinals.

Numbers can be utilized to show many alternative types of data and outcomes. When numbers are introduced to customers, they usually want be introduced in a format that’s extra readable — resembling including ordinal suffix (“June twelfth” moderately than “June 12”, for instance).

Ordinal Suffix Guidelines in English

Let’s check out how ordinals are used within the English language. English ordinals comply with a predictable, if not fantastically easy, algorithm:

  • “st” is appended to 1 and numbers which are one larger than a a number of of ten, apart from 11 and numbers which are 11 larger than a a number of of 100. For instance, 1st, twenty first, thirty first, and so forth. … however eleventh, 111th, and so forth.

  • “nd” is appended to 2 and numbers which are two larger than a a number of of ten, apart from 12 and numbers which are 12 larger than a a number of of 100. For instance, 2nd, twenty second, thirty second, and so forth. … however twelfth, 112th, and so forth.

  • “rd” is appended to three and numbers which are three larger than a a number of of ten, apart from 13 and numbers which are 13 larger than a a number of of 100. For instance, third, twenty third, thirty third, and so forth. … however thirteenth, 113th, and so forth.

  • “th” is appended to the whole lot else. For instance, twenty fourth.

The right way to Get the Ordinal of a Quantity

To get the ordinal of a quantity, you should utilize the next operate:

operate 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;

The operate getOrdinal accepts an argument that may be a quantity and returns the ordinal of that quantity. Since most ordinals finish in “th”, the default worth of ord is about to th. Then, you take a look at the quantity on completely different circumstances and alter the ordinal if needed.

You’ll discover that in every of the circumstances the remainder (%) operator is used. This operator returns the leftover worth of dividing the left operand by the proper operand. For instance, 112 % 100 returns 12.

To check if the quantity ought to have the ordinal st, you verify if n is one larger than a a number of of ten (n % 10 == 1, which incorporates 1 itself), however isn’t 11 larger than a a number of of 100 (n % 100 != 11, which incorporates 11 itself).

To check if the quantity ought to have the ordinal nd, you verify if n is 2 larger than a a number of of ten (n % 10 == 2 which incorporates 2 itself), however isn’t 12 larger than a a number of of 100 (n % 100 != 12, which incorporates 12 itself).

To check if the quantity ought to have the ordinal rd, you verify if n is 3 larger than a a number of of ten (n % 10 == 3, which incorporates 3 itself), however isn’t 13 larger than a a number of of 100 (n % 100 != 13, which incorporates 13 itself).

If all the circumstances are false, then the worth of ord stays th.

You may take a look at it in reside motion with the next CodePen demo.

See the Pen
Get the Ordinal of a Number
by SitePoint (@SitePoint)
on CodePen.

Conclusion

On this tutorial, you’ve realized how one can retrieve the ordinal of a quantity. Ordinals can be utilized in number of circumstances, resembling displaying dates or rating in human-readable codecs.

Should you discovered this text helpful, you may additionally get pleasure from the next:



Source link

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

An Introduction to Lodash and Its Benefits for JavaScript Developers – MUO – MakeUseOf

by learningcode_x1mckf
February 6, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

An Introduction to Lodash and Its Benefits for JavaScript Developers  MUO - MakeUseOf Source link

Read more

Mimic Javascript actions on identical element? – JavaScript – SitePoint

by learningcode_x1mckf
February 6, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Mimic Javascript actions on identical element? - JavaScript  SitePoint Source link

Read more

How To Hire a Professional JavaScript Developer and Find the Best … – Intelligent Living

by learningcode_x1mckf
February 6, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

How To Hire a Professional JavaScript Developer and Find the Best ...  Clever Residing Source link

Read more

How to Use Regular Expressions in JavaScript – MUO – MakeUseOf

by learningcode_x1mckf
February 6, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

How to Use Regular Expressions in JavaScript  MUO - MakeUseOf Source link

Read more

Web Development & JavaScript Trends in 2023 – Electronicsmedia

by learningcode_x1mckf
February 6, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Web Development & JavaScript Trends in 2023  Electronicsmedia Source link

Read more
Next Post
Moving Your Data Beyond Flat Files – Real Python

Moving Your Data Beyond Flat Files – Real Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

Stealthy Malware Written in C/C++ Steal Directories

Stealthy Malware Written in C/C++ Steal Directories

October 19, 2022
ClockKit complications cheatsheet – The.Swift.Dev.

ClockKit complications cheatsheet – The.Swift.Dev.

October 12, 2022
ArchUnit Verifies Architecture Rules for Java Applications

ArchUnit Verifies Architecture Rules for Java Applications

October 17, 2022

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • C++ Is TIOBE's Language Of The Year – iProgrammer
  • JobRunr, the Java Scheduler Library, Released Version 6.0 – InfoQ.com
  • An Introduction to Lodash and Its Benefits for JavaScript Developers – MUO – MakeUseOf

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?