How can i implement 'k' or 'm' prefixer to my code? I am not sure how to add it to my code.
i want the number 1000 to display as 1k how can i make the prefixer in my code. can someone please help me add it to my code please.
How can i implement 'k' or 'm' prefixer to my code? I am not sure how to add it to my code.
i want the number 1000 to display as 1k how can i make the prefixer in my code. can someone please help me add it to my code please.
To use a thousands separator, you can use the Intl.NumberFormat
method. For instance Sweden uses the space as thousands separator, so you could reference that:
console.log(new Intl.NumberFormat('se').format(1000000));
For a way to format a number with K or M suffixes, see this complete answer.
if (number >= 1000000) {
document.write((number / 1000000).toFixed(2) + "M")
}
else if (number >= 1000) {
document.write((number / 1000).toFixed(2) + "K");
}
else {
document.write(number)
}