javascript numerals add k with 4 digits code example
Example 1: convert number to k m b javascript
function nFormatter(num) {
if (num >= 1000000000) {
return (num / 1000000000).toFixed(1).replace(/\.0$/, '') + 'G';
}
if (num >= 1000000) {
return (num / 1000000).toFixed(1).replace(/\.0$/, '') + 'M';
}
if (num >= 1000) {
return (num / 1000).toFixed(1).replace(/\.0$/, '') + 'K';
}
return num;
}
Example 2: javascript convert number from thousands to k and millions to m
function numFormatter(num) {
if(num > 999 && num < 1000000){
return (num/1000).toFixed(1) + 'K';
}else if(num > 1000000){
return (num/1000000).toFixed(1) + 'M';
}else if(num < 900){
return num;
}
}