0

I'm working on a calculator app. Everything is running fine but it runs into problem when I try to compute difference between to numbers with decimal. The issue is coming from method I used to convert string into number. e.g. parseFloat("0.584") - parseFloat("0.94801") -0.36401000000000006

Ideally the output should be -0.36401 but there unnecessary zeros with a 6 at the end.

As a temp fix I've used toFixed(5) in the end. (parseFloat("0.584") - parseFloat("0.94801")).toFixed(5) '-0.36401' But that limits my calculator to be precise up to 5 decimal places

Is there a better way to convert strings into float with better precision?

  • No, you don't want to use float because float can not store decimals accurately no matter how you convert them. Find some decimal numbers library – mousetail Apr 01 '23 at 05:37
  • Hi you can use Number like this : const num1 = Number("0.584"); – Jagroop Apr 01 '23 at 05:44
  • Using Number ends up with same problem Number("0.584") - Number("0.94801") -0.36401000000000006 – Ankit Sharma Apr 01 '23 at 09:17
  • It's a common issue with float numbers and is not a Javascript matter only. Please follow this link may help you. https://stackoverflow.com/questions/3612744/remove-insignificant-trailing-zeros-from-a-number. As far as I know there is no exact way to solve this issue but using round or toFixed (or set precision). – Raeisi Apr 01 '23 at 11:13

0 Answers0