Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Appending Suffix to Numbers in JavaScript
Appending suffixes to numbers in JavaScript is used to add ordinal indicators (like "st", "nd", "rd", and "th") to the end of a number to denote its position in a sequence. This is useful for displaying dates, rankings, or other numeric data.
A custom function can be created to append the correct suffix to a given number.
The function handles special cases for numbers ending in 11, 12, and 13, and applies the appropriate suffix based on the last digit for other numbers.
Rules for Ordinal Suffixes
The task of our function is to append 'st', 'nd', 'rd', 'th' to the number according to the following rules:
- st is used with numbers ending in 1 (e.g. 1st, 21st, 101st)
- nd is used with numbers ending in 2 (e.g. 2nd, 22nd, 102nd)
- rd is used with numbers ending in 3 (e.g. 3rd, 23rd, 103rd)
- Exception: Numbers ending with 11, 12, or 13 always use th (e.g. 11th, 12th, 13th, 111th, 112th, 113th)
- th is used for all other numbers (e.g. 4th, 5th, 9th, 20th)
Sample Input:
const num = 4513;
Sample Output:
4513th
Explanation: Even though 4513 ends with 3, the last two digits form 13, which is an exception case that must be appended with "th".
Algorithm Implementation
Here's the core function that determines the correct suffix for any number:
function getOrdinalSuffix(num) {
// Handle zero as special case
if (num === 0) return "";
// Check for teen exceptions (11, 12, 13, 111, 112, 113, etc.)
if (num % 100 === 11 || num % 100 === 12 || num % 100 === 13) {
return "th";
}
// Check last digit for regular cases
const lastDigit = num % 10;
if (lastDigit === 1) return "st";
if (lastDigit === 2) return "nd";
if (lastDigit === 3) return "rd";
// Default case
return "th";
}
function appendSuffix(num) {
return num + getOrdinalSuffix(num);
}
// Test the function
console.log(appendSuffix(1)); // 1st
console.log(appendSuffix(2)); // 2nd
console.log(appendSuffix(3)); // 3rd
console.log(appendSuffix(11)); // 11th
console.log(appendSuffix(21)); // 21st
console.log(appendSuffix(113)); // 113th
1st 2nd 3rd 11th 21st 113th
Example: Single-Digit Numbers
Let's test our function with single-digit numbers from 1 to 9:
const appendText = (num = 1) => {
let suffix = "th";
if (num === 0) suffix = "";
if (num % 10 === 1 && num % 100 !== 11) suffix = "st";
if (num % 10 === 2 && num % 100 !== 12) suffix = "nd";
if (num % 10 === 3 && num % 100 !== 13) suffix = "rd";
return num + suffix;
};
const singleDigits = [1, 2, 3, 4, 5, 6, 7, 8, 9];
singleDigits.forEach(num => console.log(appendText(num)));
1st 2nd 3rd 4th 5th 6th 7th 8th 9th
Example: Special Teen Cases
The teen numbers (11, 12, 13) and their multiples (111, 112, 113, etc.) are exceptions that always use "th":
const appendText = (num = 1) => {
let suffix = "th";
if (num === 0) suffix = "";
if (num % 10 === 1 && num % 100 !== 11) suffix = "st";
if (num % 10 === 2 && num % 100 !== 12) suffix = "nd";
if (num % 10 === 3 && num % 100 !== 13) suffix = "rd";
return num + suffix;
};
const teenNumbers = [11, 12, 13, 111, 112, 113, 211, 212, 213];
teenNumbers.forEach(num => console.log(appendText(num)));
11th 12th 13th 111th 112th 113th 211th 212th 213th
Example: Mixed Large Numbers
Testing with various large numbers to demonstrate the function works correctly across different ranges:
const appendText = (num = 1) => {
let suffix = "th";
if (num === 0) suffix = "";
if (num % 10 === 1 && num % 100 !== 11) suffix = "st";
if (num % 10 === 2 && num % 100 !== 12) suffix = "nd";
if (num % 10 === 3 && num % 100 !== 13) suffix = "rd";
return num + suffix;
};
const mixedNumbers = [21, 22, 23, 101, 102, 103, 121, 122, 123, 1001];
mixedNumbers.forEach(num => console.log(appendText(num)));
21st 22nd 23rd 101st 102nd 103rd 121st 122nd 123rd 1001st
Practical Use Cases
Here are some common scenarios where ordinal suffixes are useful:
function formatDate(day, month, year) {
const months = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'];
const appendText = (num) => {
let suffix = "th";
if (num % 10 === 1 && num % 100 !== 11) suffix = "st";
if (num % 10 === 2 && num % 100 !== 12) suffix = "nd";
if (num % 10 === 3 && num % 100 !== 13) suffix = "rd";
return num + suffix;
};
return `${months[month - 1]} ${appendText(day)}, ${year}`;
}
console.log(formatDate(1, 1, 2024)); // January 1st, 2024
console.log(formatDate(22, 12, 2023)); // December 22nd, 2023
console.log(formatDate(13, 6, 2024)); // June 13th, 2024
January 1st, 2024 December 22nd, 2023 June 13th, 2024
Conclusion
Appending ordinal suffixes to numbers in JavaScript requires handling special cases for teen numbers (11, 12, 13) while applying standard rules for other digits. This functionality is essential for creating user-friendly displays of rankings, dates, and sequential data.
