Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

297
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda