Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

227
Vistas
How do i change a number into fixed notation, like K, M, or B, in Javascript?

I'm trying to change a number into a fixed notation, for example when the number is over 1000, change the number into x.x K, and so far everything i have tried has failed. You can see my code if you go to this link: https://jsfiddle.net/Blackcatgame77/vnzewfr1/7/ . I have a function ready to change my number into notation, here is the function

function formatter(cash) {
     if (cash >= 1000000000) {
        return (cash / 1000000000).toFixed(1).replace(/\.0$/, '') + 'B';
     }
     if (cash >= 1000000) {
        return (cash / 1000000).toFixed(1).replace(/\.0$/, '') + 'M';
     }
     if (cash >= 1000) {
        return (cash / 1000).toFixed(1).replace(/\.0$/, '') + 'K';
     }
}

I'm not sure how to use this function to make the numbers change into notation when they reach 1000, 1000000, or 1000000000. Can someone tell me how i could do this?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I managed to figure it out, and here's how i did it. I defined a second variable from the currency called cashE, and then changed the variable being displayed from cash to cashE. To notate cash, i used this code:

  let cashE = cash;
  if (cash >= 1e+3) {
    cashE = (cash / 1e+3).toFixed(1).replace(/\.0$/, '') + 'K';
  }
  if (cash >= 1e+6) {
    cashE = (cash / 1e+6).toFixed(1).replace(/\.0$/, '') + 'M';
  }
  if (cash >= 1e+9) {
    cashE = (cash / 1e+9).toFixed(1).replace(/\.0$/, '') + 'B';
  }

I hope this helps anyone who was wondering how to do this :)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda