0

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.

2 Answers2

3

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.

trincot
  • 317,000
  • 35
  • 244
  • 286
2
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)
}
Tomer Wolberg
  • 1,256
  • 10
  • 22