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

272
Visualizações
How do you round down to one decimal, even if it ends with a 0 in JavaScript?

I have a product page that is fetching products information and displaying the products on the page. The API returns three price points. I have to add them up and display the price rounded down to one decimal. I have a solution already working, but it only works if it's not a whole number.

22.7312 returns 22.7, but 22.0 returns 22. I would like to always show one decimal, even if it's a zero. This is my current solution. How can I change it so that it shows the one decimal, even if it's a zero?

Math.floor(
    (parseFloat(product.price1['regionalPrice'])
      + parseFloat(product.price2['marketPrice'])
      + parseFloat(product.price3['localPrice'])
    ) * 10) / 10
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to format your float result with the .toFixed(x) method.

Here is a full code sample for this:

const p1 = parseFloat(product.price1['regionalPrice']);
const p2 = parseFloat(product.price2['marketPrice']);
const p3 = parseFloat(product.price3['localPrice']);

const price = Math.floor((p1 + p2 + p3) * 10) / 10;
const displayPrice = price.toFixed(1);
about 4 years ago · Juan Pablo Isaza Relatório

0

You could try rounding your numbers with the number.toFixed() function where you pass 1 as a function argument. I tried it and
num = 22; num = nums.toFixed(1); console.log(num)
prints out 22.0. Hope that this is what you were looking for ^^

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