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

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

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 Report

0

y = -8985.915

y.toFixed(2) => -8985.92

about 4 years ago · Juan Pablo Isaza Report

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