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

271
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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