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

119
Vistas
parseFloat causing weird calculations

I have a function that needs to find the difference between 3 numbers. They can come in as strings, so I need to use parseFloat and maintain 2 decimal places. When I use parseFloat with negative numbers, I'm getting weird differences though.

// This will output -0.004999999999636212
let temp = -8985.69 - -8985.915 - 0.23
console.log(temp);

// But this will output -4.365674488582272e-13
let x = -8985.69
let y = -8985.915
let z = 0.23
let temp2 = parseFloat(x.toFixed(2)) - parseFloat(y.toFixed(2)) - parseFloat(z.toFixed(2))
console.log(temp2);

I was looking at the parseFloat docs and I see the part where the conversion may get changed when an invalid character is encountered. But I'm not seeing any wrong characters here and I'm not getting how the parsing stopping at any point would cause it to equal -4.365674488582272e-13

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

0

You second number has three decimal places and you set it toFixed(2) so it changes its value to -8985.92. When I use .toFixed(3) for it I get the first result you are looking for.

about 4 years ago · Juan Pablo Isaza Denunciar

0

y = -8985.915

y.toFixed(2) => -8985.92

about 4 years ago · Juan Pablo Isaza Denunciar

0

You need to read David Goldberg's 1991 paper, What Every Computer Scientist Should Know About Floating-Point Arithmetic

And visit this web site: What Every Programmer Should Know About Floating-Point Arithmetic, or, Why don’t my numbers add up?

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