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

333
Vistas
is that parseFloat(string).tofixed(2) will cause me a problem?

I have some data like RM 28.51 (Currency), RM30.28(Currency) in my table and need to use these data to do some calculation

my solution is to parseFloat(28.51).tofixed(2) + parseFloat(30.28).tofixed(2)

but when the moment more and more data come in , sometimes they will some decimal point error shown as result

Example my result at the end will show 3859.39 but the actual is 3859.40

Is there any better solution?

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

0

parseFloat(28.51).toFixed(2) + parseFloat(30.28).toFixed(2) - sometimes they will some decimal point error shown as result

The JavaScript toFixed method takes in a number and outputs a string

When you use + with two numbers, you add both numbers.

When you use + with two strings, you join both strings together.

The example below illustrates the difference.

function addTwoStrings(value1, value2) {
  return parseFloat(value1).toFixed(2) + parseFloat(value2).toFixed(2)
}
function addTwoNumbers(value1, value2) {
  return (parseFloat(value1) + parseFloat(value2)).toFixed(2)
}

console.log(addTwoStrings(28.514, 30.284))
console.log(addTwoNumbers(28.514, 30.284))

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