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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
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
CodePudding user response:
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.
CodePudding user response:
y = -8985.915
y.toFixed(2) => -8985.92
CodePudding user response:
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?