Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript convert number from thousands to k and millions to m

// converts number to string representation with K and M.
// toFixed(d) returns a string that has exactly 'd' digits
// after the decimal place, rounding if necessary.
function numFormatter(num) {
    if(num > 999 && num < 1000000){
        return (num/1000).toFixed(1) + 'K'; // convert to K for number from > 1000 < 1 million 
    }else if(num > 1000000){
        return (num/1000000).toFixed(1) + 'M'; // convert to M for number from > 1 million 
    }else if(num < 900){
        return num; // if value < 1000, nothing to do
    }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to upgrade to react 18 
Javascript :: replacing characters in string javascript 
Javascript :: Unhandled rejection TypeError: Article.findById is not a function sequelize 
Javascript :: load json 
Javascript :: your company assigns each customer a membership id 
Javascript :: send a message when a bot joins your server discord.js 
Javascript :: execute code after page load javascript 
Javascript :: como deletar node js linux 
Javascript :: jquery slidetoggle 
Javascript :: dont drag img 
Javascript :: javascript modal close 
Javascript :: javascript reference file two folders up 
Javascript :: load jquery in the browser code 
Javascript :: keydown jquery 
Javascript :: node list files in directory 
Javascript :: javascript to mask email address 
Javascript :: remove negative sign from number javascript 
Javascript :: a quick introduction to pipe and compose javascript 
Javascript :: prepend element jquery 
Javascript :: string pop last char js 
Javascript :: disable input angular 
Javascript :: js wrap an element 
Javascript :: convert json to base64 javascript 
Javascript :: js console.log color reset 
Javascript :: page redirect after a few seconds 
Javascript :: javascript detect square number 
Javascript :: jquery check input is disable 
Javascript :: js for of array with index 
Javascript :: router-link vue 
Javascript :: html-webpack-plugin npm 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =