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

129
Visualizações
Always hide .00 decimal number

I would like to format my decimal numbers to display 2 decimal places but .00 will always hide.

example

value      expect     return     
------     -------    ------
1.00       1           1.00
1          1           1.00
1.6        1.60        1.60
1.451      1.45        1.45



My Code

Vue.filter("currency", value => {
    return parseFloat(value).toFixed(2);
});
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Remove .00 from the end if it exists:

 return parseFloat(value).toFixed(2).replace(/\.00$/, '')
about 4 years ago · Juan Pablo Isaza Relatório

0

You can simply put the toFixed INTO the parseFloat like this:

Vue.filter("currency", value => {
    return parseFloat(value.toFixed(2))
});

toFixed will round/pad the number to a specific length, but also convert it to a string. Converting that back to a numeric type will not only make the number safer to use arithmetically, but also automatically drop any trailing 0's.

Reference: https://newbedev.com/remove-insignificant-trailing-zeros-from-a-number

Update

This solution doesn't consider the case: 1.6 -> 1.60 (check jsfiddle in the comment)

about 4 years ago · Juan Pablo Isaza Relatório

0

Just check whether the number is wholenumber by checking the reminder on division with 1

Vue.filter("currency", value => {
    return parseFloat(value) % 1 === 0 ? parseFloat(value).toString() : parseFloat(value).toFixed(2);
});

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