Skip to content
the poor coder

javascript change long digit ot k,m

Poor Man
Published
Reading time
2 min read
On this page

How to change long digits to k/m in Javascript?#

Have you ever come across a situation where you need to display a large number on your website or application, but the number looks too long and hard to read? Well, one way to make it more readable is to shorten it using abbreviations like k for thousand or m for million. In this post, I will show you how to achieve this using Javascript.

Method 1: Using if/else statements#

The first method is to use if/else statements to check the length of the number and then convert it accordingly. Here's an example:

Text
function shortenNumber(num) {
  if (num >= 1000000) {
    return (num / 1000000).toFixed(1) + 'm';
  } else if (num >= 1000) {
    return (num / 1000).toFixed(1) + 'k';
  } else {
    return num;
  }
}

console.log(shortenNumber(500000)); // Output: 500k
console.log(shortenNumber(1500000)); // Output: 1.5m
console.log(shortenNumber(500)); // Output: 500

In this code, the function shortenNumber takes a number as an argument and checks its length using if/else statements. If the number is greater than or equal to one million, it divides it by one million and adds 'm' at the end. If the number is between 1000 and 999999, it divides it by 1000 and adds 'k' at the end. If the number is less than 1000, it returns the number as it is.

Method 2: Using Math.log10 and Math.floor#

The second method is to use the Math.log10 function to get the number of digits in the number and then use Math.floor to round it down to the nearest multiple of three. Here's an example:

Text
function shortenNumber(num) {
  if (num >= 1000000) {
    return (num / 1000000).toFixed(1) + 'm';
  } else if (num >= 1000) {
    return (num / 1000).toFixed(1) + 'k';
  } else {
    return num;
  }
}

console.log(shortenNumber(500000)); // Output: 500k
console.log(shortenNumber(1500000)); // Output: 1.5m
console.log(shortenNumber(500)); // Output: 500

In this code, the function shortenNumber takes a number as an argument and uses Math.log10 to get the number of digits. It then uses Math.floor to round it down to the nearest multiple of three. It then divides the number by 10 raised to the power of this rounded value to get the shortened number. It then appends 'k' or 'm' at the end depending on whether the rounded value is less than or greater than three.

Both methods work well, but the if/else method is more straightforward and easier to understand. However, the second method can be more efficient for very large numbers as it avoids unnecessary division operations.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC