-1

On my site, I've got a custom field on the backend of posts where I can enter a number, in this case, revenue. It's entered as a raw number, like 1000000. There are some numbers that go as high as one billion. I'd like to add a code somehow that lets the front end number appear as $1M or $1B instead of the long-form of the number. Is there a code I can create/use to help me do this? And would it go into the css or the html? It's important that the number on the backend remain a raw number, because I've got post filtering set up based on those numbers.

  • Does this answer your question? [1000000 to 1M and 1000 to 1K and so on in JS](https://stackoverflow.com/questions/9345136/1000000-to-1m-and-1000-to-1k-and-so-on-in-js) – Albin Paul Jul 09 '21 at 20:26

1 Answers1

0

This should work, the real price remains in the variable so you can work with it later

let price = 3030000;

if (price >= 1000000) {
  document.getElementById("price").innerHTML = "$" + (price / 1000000) + "M";
}else if (price >= 1000) {
  document.getElementById("price").innerHTML = "$" + (price / 1000) + "K";
}else {
  document.getElementById("price").innerHTML = "$" + price;
}
<p id="price"></p>
AlexSp3
  • 2,201
  • 2
  • 7
  • 24