Best Website-BuildersBest Website-Builders
    What's Hot

    Steel Barricades Go up Outside Trump Tower As Trump Indictment Looms

    March 22, 2023

    5 Best Deals From the Wyze Spring Sale: Video Doorbells and Security Cameras

    March 22, 2023

    Reddit – Dive into anything

    March 22, 2023
    Facebook Twitter Instagram
    Facebook Twitter Instagram
    Best Website-BuildersBest Website-Builders
    • Home
    • CSS

      Social Security launches investigation into disappearance of zombie drug

      March 22, 2023

      The Markland area will be closed for about six months

      March 21, 2023

      The battle of ironclads marked the beginning of modern ships

      March 21, 2023

      CDJapan : Pale Moon 32.1.0

      March 21, 2023

      A great SSG just got better

      March 21, 2023
    • Joomla

      Reseller Hosting Business: Important Q&A

      March 21, 2023

      Web Hosting: 8 Elements Every Entrepreneur Should Look For

      March 20, 2023

      VS Code Extension for In-Browser Development, WapuuGotchi Gamification Plugin & More – WP Tavern

      March 20, 2023

      How Superior Web Hosting Support Can Drive Business Success

      March 17, 2023

      PANDACU Studio Website Development Cooperation First Page Sage SEO Dsign Chicago adstargets Cardinal Digital Agency

      March 16, 2023
    • PHP

      Maren Morris introduces son to drag queen and unapologetically shares support for LGBTQ community at Tennessee benefits show

      March 21, 2023

      Gwyneth Paltrow ski crash lawsuit trial begins

      March 21, 2023

      “Anything exciting today?”

      March 21, 2023

      Possible New York Trump indictment attracts demonstrators

      March 21, 2023

      Here are the best cheap wireless earbuds under $25 on Amazon

      March 21, 2023
    • UX

      Adobe Explodes Its AI

      March 22, 2023

      ‘Laws are working as intended’: Pay transparency brings new awkwardness to the workplace

      March 21, 2023

      Microsoft says PlayStation should make an exclusive to compete with Call of Duty

      March 21, 2023

      How a holistic UX approach adds value to the design process

      March 21, 2023

      Spectrum Access app spotlighted for digital inclusion

      March 21, 2023
    • Web Builders
      1. Web Design
      2. View All

      What Comes First in Website Development — Design or Copy?

      February 2, 2023

      Modern Campus Honors Best Higher Education Websites of 2022

      February 2, 2023

      Premier SEO Consultant in Las Vegas, Nevada with Unparalleled Customer Service

      February 2, 2023

      Can Religious Freedom Be Saved? This group is racing the clock to teach America’s first freedom

      February 2, 2023

      How i Create New Google Account

      February 7, 2023

      CWT powers tools for meeting and event planners

      January 31, 2023

      Best Website Builder – Website Builders

      January 24, 2023

      Is There A Market For Rap-Themed Slot Games? – Rap Review

      January 19, 2023
    • WordPress

      Is Duolingo developing a new music-learning app? We interpret the signs

      March 21, 2023

      The latest news, rumors and everything we know so far | Tech Radar

      March 21, 2023

      The Pro version of the Nvidia RTX 4090 Mobile may be a bit of a disappointment

      March 21, 2023

      Russia orders civil servants to ditch iPhones

      March 21, 2023

      Crypto stealers target .NET developers in new campaign

      March 21, 2023
    • Realtoz
      • Our Other Sites
    • More News
    Best Website-BuildersBest Website-Builders
    Home » Quick Tip: How to Convert Numbers to Ordinals in JavaScript
    JavaScript

    Quick Tip: How to Convert Numbers to Ordinals in JavaScript

    websitebuildersnowBy websitebuildersnowSeptember 8, 2022No Comments4 Mins Read
    Facebook Twitter LinkedIn Telegram Pinterest Tumblr Reddit WhatsApp Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    In this tutorial, you’ll learn how to convert numbers to ordinals in JavaScript. Once you have the ordinal of the number, you can display it in human readable form.

    What is an ordinal

    Ordinals define numbers as part of an order or sequence. The words “first”, “second”, and “third” are all examples of ordinal numbers. When using numbers to display chart results, dates of the month, or rankings, it is often necessary to use ordinal numbers.

    Numeric values ​​can be used to display different types of data and results. When presenting a number to the user, it is often necessary to present it in a more readable format, such as by adding an ordinal suffix (for example, “June 12th” instead of “June 12”).

    English Ordinal Suffix Rules

    Let’s see how ordinal numbers are used in English. Ordinal numbers in English follow a set of predictable, if not beautifully simple, rules.

    • Numbers that are one more than a multiple of 1 and 10 are marked with ‘st’. Except for 11 and 11 being a multiple of 100. For example, 1st, 21st, 31st, etc.Such

    • ‘nd’ is added to numbers 2 greater than multiples of 2 and 10, except for 12 which is greater than multiples of 12 and 100.Such

    • ‘rd’ is added to numbers of 3 greater than multiples of 3 and 10, except for 13 which is greater than multiples of 13 and 100.Such

    • The ‘th’ is appended to everything else. For example, 24 days.

    How to get the ordinal of a number

    To get the ordinal of a number you can use the following functions:

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

    function getOrdinal Accepts an argument that is a number and returns the ordinal of that number.Since most ordinal numbers end in “th”, the default value of ord is set to thThen test the numbers under different conditions and change the ordinal if necessary.

    Notice that each condition uses the modulo (%) operator. This operator returns the remainder of dividing the left operand by the right operand. for example, 112 % 100 Return value 12.

    To test if a number requires an ordinal stto see if n is 1 greater than a multiple of 10 (n % 10 == 1including 1 itself), provided that 11 is not greater than a multiple of 100 (n % 100 != 11which includes 11 itself).

    To test if a number requires an ordinal ndto see if n 2 is greater than a multiple of 10 (n % 10 == 2 2 itself), but 12 is not greater than a multiple of 100 (n % 100 != 12which includes 12 itself).

    To test if a number requires an ordinal rdto see if n 3 is greater than a multiple of 10 (n % 10 == 3including 3 itself), provided that 13 is not greater than a multiple of 100 (n % 100 != 13which includes 13 itself).

    If all conditions are false, ord Remains th.

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

    look at the pen
    Get number ordinal in SitePoint (@SitePoint)
    with a code pen.

    Conclusion

    In this tutorial, you learned how to get the ordinal of a number. Ordinal numbers can be used in a variety of ways, such as displaying dates and ranking in human-readable form.

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



    Source link

    Share this:

    • Tweet
    • Email
    • Pocket
    • Mastodon
    • WhatsApp
    • Telegram
    • Share on Tumblr
    • Print
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Telegram Email
    Previous ArticleEast Texas News – Colmesneil Bulldogs steamroll KIPP
    Next Article The Daily Herald – Anguilla observes passing of the late Queen Elizabeth II
    websitebuildersnow
    • Website

    Related Posts

    Move away from using (most) WebR R functions in WebR-powered apps/sites and call a single JavaScript function

    March 21, 2023

    “Smaller, Simpler, Faster” — Visual Studio Magazine

    March 21, 2023

    Burp Suite Enterprise Edition Power Tools: Unleash the Power of Command Line, Python and More | Blog

    March 21, 2023
    Add A Comment

    Leave a Reply Cancel reply

    Top Posts

    Subscribe to Updates

    Get the latest sports news from SportsSite about soccer, football and tennis.

    Advertisement
    Demo

    This website provides information about CSS and other things. Keep Supporting Us With the Latest News and we Will Provide the Best Of Our To Makes You Updated All Around The World News. Keep Sporting US.

    Facebook Twitter Instagram Pinterest YouTube
    Top Insights

    Steel Barricades Go up Outside Trump Tower As Trump Indictment Looms

    March 22, 2023

    5 Best Deals From the Wyze Spring Sale: Video Doorbells and Security Cameras

    March 22, 2023

    Reddit – Dive into anything

    March 22, 2023
    Get Informed

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    © 2023 bestwebsite-builders. Designed by bestwebsite-builders.
    • Home
    • About us
    • Contact us
    • DMCA
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.