JavaScript provides several ways to convert large numbers into compact, readable strings such as 1.2K, 1.2M, and 1.2B. This can be achieved using built-in methods, custom functions, logarithms, recursion, or external libraries.
- Use Intl.NumberFormat() for a simple built-in solution with locale-aware formatting.
- Use a custom function or logarithms when you need control over suffixes and precision.
- Use recursion or libraries such as Numeral.js when reusable number-formatting logic is required.
Approach 1: Using JavaScript Methods
This approach determines the appropriate suffix based on the number of digits and divides the number by the corresponding power of 1000.
Example: In this example, we use an array of suffixes and JavaScript number methods to abbreviate the given number.
// Input number
let n = 123287342;
// Function to convert number
function convert(val) {
// Thousands, millions, billions, etc.
let s = ["", "k", "m", "b", "t"];
// Determine the suffix index
let sNum = Math.floor(("" + val).length / 3);
// Calculate the abbreviated value
let sVal = parseFloat(
(sNum !== 0
? val / Math.pow(1000, sNum)
: val
).toPrecision(2)
);
if (sVal % 1 !== 0) {
sVal = sVal.toFixed(1);
}
// Append the suffix
return sVal + s[sNum];
}
// Display converted output
function GFG_Fun() {
console.log("Number = " + convert(n));
}
GFG_Fun();
Note: This digit-length approach is simple but is not ideal for values close to suffix boundaries, such as 999999, because the suffix is determined from the number of digits rather than its actual magnitude.
Approach 2: Using a Custom Function
A custom function can check the range of the number and divide it by 1000, 1e6, 1e9, or 1e12 accordingly.
Example: In this example, we use a custom function to convert a number into K, M, B, or T notation.
// Input number
let n = 1232873425;
// Function to convert
let convert = (n) => {
if (n < 1e3) return n;
if (n < 1e6)
return +(n / 1e3).toFixed(1) + "K";
if (n < 1e9)
return +(n / 1e6).toFixed(1) + "M";
if (n < 1e12)
return +(n / 1e9).toFixed(1) + "B";
return +(n / 1e12).toFixed(1) + "T";
};
// Display converted output
function GFG_Fun() {
console.log("Number = " + convert(n));
}
GFG_Fun();
Approach 3: Using Logarithms
The Math.log10() method can determine the order of magnitude of a number. Dividing this value by 3 gives the appropriate index for the suffix array.
Example: In this example, logarithms are used to determine whether the number should be represented using K, M, B, or T.
// Input number
let n = 1232873425;
// Function to convert
let convert = (n) => {
// Abbreviation array
let s = ["", "k", "m", "b", "t"];
// Find the order of magnitude
let orderOfMagnitude = Math.floor(Math.log10(n));
// Determine the suffix index
let index = Math.floor(orderOfMagnitude / 3);
// Calculate abbreviated value
let abbreviatedValue = parseFloat(
(n / Math.pow(1000, index)).toPrecision(2)
);
// Append the abbreviation
return abbreviatedValue + s[index];
};
// Display converted output
function GFG_Fun() {
console.log("Number = " + convert(n));
}
GFG_Fun();
Note: For production code, the function should also handle 0, negative numbers, and values beyond the supported suffix array.
Approach 4: Using Intl.NumberFormat()
The Intl.NumberFormat() object provides built-in support for compact number notation. The notation: "compact" option automatically converts large numbers into abbreviated forms.
Example: In this example, we use Intl.NumberFormat() to format numbers using compact notation.
function convertToAbbreviation(number) {
const formatter = new Intl.NumberFormat("en", {
notation: "compact",
compactDisplay: "short",
maximumSignificantDigits: 3
});
return formatter.format(number);
}
console.log(convertToAbbreviation(1234));
console.log(convertToAbbreviation(1234567));
console.log(convertToAbbreviation(1234567890));
console.log(convertToAbbreviation(1234567890123));
This is generally the simplest modern approach when locale-aware compact formatting is sufficient.
Approach 5: Using Recursion
A recursive function can repeatedly divide the number by 1000 until it becomes smaller than 1000. The appropriate suffix is selected at each recursive step.
Example: In this example, recursion is used to convert the number into an abbreviated representation.
// Input number
let n = 1232873425;
// Recursive function
function convert(n, index = 0) {
const suffixes = ["", "K", "M", "B", "T"];
if (n < 1000 || index === suffixes.length - 1) {
return +(n.toFixed(1)) + suffixes[index];
}
return convert(n / 1000, index + 1);
}
// Display converted output
function GFG_Fun() {
console.log("Number = " + convert(n));
}
GFG_Fun();
Approach 6: Using External Library: Numeral.js
Numeral.js provides convenient formatting options for abbreviating numbers. The format('0.0a') pattern represents thousands, millions, billions, and other large values using suffixes.
Example: In this example, we use Numeral.js to convert a long number into an abbreviated string.
// Import the numeral library
const numeral = require("numeral");
// Example long number
const number = 1234567890;
// Convert the number to an abbreviated string
const abbreviatedNumber = numeral(number).format("0.0a");
// Display the result
console.log(abbreviatedNumber);
Output:
1.2b