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

295
Vistas
I wan't to format a string of numbers as a currency in javascript but i don't know how to format my string

so what i mean is:

1000 should become 10,00
10000 should become 100,00
100000 should become 1000,00

Can some help me out?

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

0

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString#using_options

var number = 1000;

// request a currency format
console.log(number.toLocaleString('en-US', { style: 'currency', currency: 'USD' }));

Without methods and just formatting with commas

        function addComma(value) {
            return value.split('').map((v, i) => {
                if ( i === value.length - 2) {
                    return ',' + v
                }
                return v
            }).join('')
        }
        console.log(addComma('1000'))
        console.log(addComma('10000'))
        console.log(addComma('100000'))
about 4 years ago · Juan Pablo Isaza Denunciar

0

https://www.delftstack.com/howto/javascript/javascript-currency-format/

you can transform your string in number and then use toLocaleString()

here is an example from the webpage

const number = 2000;
number.toLocaleString('en-IN', {style: 'currency',currency: 'INR', minimumFractionDigits: 2})
console.log(number);
about 4 years ago · Juan Pablo Isaza Denunciar

0

Another solution, just splitting the string into whole and cents:

function addComma(n)
{
    if (n.length == 0) {
       return '0,00';
    }

    if (n.length == 1) {
       return '0,0' + n;
    }

    if (n.length == 2) {
       return '0,' + n;
    }

    let whole = n.substring(0, n.length-2);
    let cents = n.substring(n.length-2);
    return whole + ',' + cents;
}
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