Skip to content

Instantly share code, notes, and snippets.

@HashirHussain
Created March 22, 2021 10:11
Show Gist options
  • Save HashirHussain/c352de3271a7805354f53055811ca386 to your computer and use it in GitHub Desktop.
Save HashirHussain/c352de3271a7805354f53055811ca386 to your computer and use it in GitHub Desktop.
Convert number to ordinal format
function numberToOrdinal(input) {
try {
if(typeof input !== 'number') {
throw('Please provide valid number');
}
const lastDigit = input % 10;
let ordinal = '';
if(lastDigit === 1 && input !== 11) {
ordinal = 'st';
} else if (lastDigit === 2 && input !== 12) {
ordinal = 'nd';
} else if (lastDigit === 3 && input !== 13) {
ordinal = 'rd';
} else {
ordinal = 'th';
}
return input + ordinal;
} catch(e) {
console.log(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment