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

297
Vistas
Java Script parseFloat() unexpected decimal places

Why when I am cast this string "2019275.159999999916180968" to number, or when used parseFloat(str). I'm always get result "2019275.16". Why it remove extra decimal places that provided in the string? And how can i keep number of decimal places, without rounding it to 2 decimal places in this case?

Amount of decimal places in the string is dynamical.

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

0

It might be worth investigating a library like decimal.js to help with this. e.g.:

Decimal.set({ precision: 24 }); // The default of 20 is not big enough for the number below so you may need to adjust this

let a = new Decimal('2019275.159999999912345');
a = a.plus(1);

let b = new Decimal('2019275.159999999912345').plus(1);
let c = Decimal.add('2019275.159999999912345', 1);

console.log(a, b, c);
<script src="https://cdnjs.cloudflare.com/ajax/libs/decimal.js/9.0.0/decimal.min.js"></script>

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use toFixed() method after parseFloat() otherwise it will return least precision equivalent to internal representation

parseFloat("2019275.159999999916180968").toFixed(18)
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