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

298
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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