Skip to content
Advertisement

the ‘0.’ keeps disappearing after clicking other numbers in calculator

So i am building a calculator. I am trying to get the decimals to display on screen. That is not the problem. After i click the decimal button; and then a random number button, the zero to the right of the decimal point disappears. I believe this is happening because i am using the replace method to get rid of the zero in the numbers function. This is a very simple implementation that i feel like i should already know. I wanted to know if anybody had any ideas?

JavaScript
JavaScript

Advertisement

Answer

Try this below code. I have added an if condition, if (!str.includes(".")) which will now only replace 0, if there is no decimal.

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement