How to uselogarithms in Javascript

This approach involves using logarithms to determine the appropriate abbreviation for the number. We can take the logarithm base 10 of the given number to find its order of magnitude, which indicates the number of digits in the number. Then, we divide this order of magnitude by 3 to determine the appropriate index in the abbreviation array. Finally, we divide the number by 1000(order of magnitude/3) and append the corresponding abbreviation from the array.

JavaScript
// Input number
let n = 1232873425;

// Display input number
console.log(n);

// 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 index in the abbreviation array
    let index = Math.floor(orderOfMagnitude / 3);
    // Calculate the abbreviated value
    let abbreviatedValue = parseFloat((n / Math.pow(1000, index)).toPrecision(2));
    // Append the abbreviation
    return abbreviatedValue + s[index];
};

// Function to display converted output
function GFG_Fun() {
    // Display output
    console.log("Number = " + convert(n));
}

// Function call
GFG_Fun();

Output
1232873425
Number = 1.2b

How to convert long number into abbreviated string in JavaScript ?

In this article, we are given a long number and the task is to convert it to the abbreviated string(eg.. 1234 to 1.2k). Here 2 approaches are discussed with the help of JavaScript.

Similar Reads

Approaches to Convert Long Number to Abbreviated String:

Table of Content Approach 1: Using JavaScript methodsApproach 2: Using Custom functionApproach 3: Using logarithmsApproach 4: Using Intl.NumberFormat...

Approach 1: Using JavaScript methods

Get the characters in an array(ar = [“”, “k”, “m”, “b”])Divide the length of the number by 3 and get the value in var(sNum).If the sNum != 0 then calculate the precise value of the number by dividing it by 1000^sNum.Append the character at index = sNum in the array, to the precise value to finally get the abbreviated number....

Approach 2: Using Custom function

Check if the number is less than 1e3, if it is then return the number as it is.If the number is greater than or equal to 1e3 and less than 1e6 then remove the last three digits and append the character ‘K’ to it.If the number is greater than or equal to 1e6 and less than 1e9 then remove the last six digits and append the character ‘M’ to it.If the number is greater than or equal to 1e9 and less than 1e12 then remove the last nine digits and append the character ‘B’ to it.If the number is greater than or equal to 1e12 remove the last 12 digits and append the character ‘T’ to it....

Approach 3: Using logarithms

This approach involves using logarithms to determine the appropriate abbreviation for the number. We can take the logarithm base 10 of the given number to find its order of magnitude, which indicates the number of digits in the number. Then, we divide this order of magnitude by 3 to determine the appropriate index in the abbreviation array. Finally, we divide the number by 1000(order of magnitude/3) and append the corresponding abbreviation from the array....

Approach 4: Using Intl.NumberFormat

In this approach, we can utilize the Intl.NumberFormat object in JavaScript to format the number according to a specified locale. By specifying a maximum significant digits count and using the notation property with the “compact” option, we can automatically abbreviate the number based on its magnitude....