Decimal problem in JS calculator (React)

Hello everyone!

Link to code;

I’m doing the javascript calculator project in react and I stumbled on a little problem with the decimal function. It generally works BUT if I use it for lets say zero-something, the zero gets removed and the decimal becomes a whole number. Like 0.5 becomes 5 or 0.3 becomes 3. I suspect that it has something to do with the digits being strings but I’ve tried to change the state to number but then it also offers some problems.

All input is appreciated.
Thanks in advance.

FOUND THE SOLUTION!

Changed

 if (display == 0) {
      setDisplay(num)
  } 

to

if (display == 0 && display.indexOf(".") == -1) {
      setDisplay(num)
 } 

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.