HackerNoon Mobile

Better reading experience on the app
JavaScript Ordinals: Adding st, nd, rd and th Suffixes to a Numberby@smpnjn
554 reads

JavaScript Ordinals: Adding st, nd, rd and th Suffixes to a Number

tldt arrow
Read on Terminal Reader
Read this story w/o Javascript

Too Long; Didn't Read

In Javascript it's usually considered best practice to work with absolute numbers. But when expressing these numbers in user interfaces, it's more likely we'll want to express them differently. Fortunately there is a solution in Javascript - the use of 'Intl.PluralRules` This will define plural rules based on locale. It will tell us language specific rules on how to handle each number - so we can do things like `2nd, `3rd` and `4th. To map our numbers to '1st' or '2nd' we only have to create a mapping like this:
featured image - JavaScript Ordinals: Adding st, nd, rd and th Suffixes to a Number
Johnny Simpson HackerNoon profile picture

@smpnjn

Johnny Simpson


Receive Stories from @smpnjn

react to story with heart

RELATED STORIES

L O A D I N G
. . . comments & more!