Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

295
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!