'I have made a simple calculator but How I can prevent users from entering dot several times in one number ? so far i can only prevent double dots
if
(previousNum == "." && currentNum == ".") { screen.value = screen.value.substring(0, numchar - 1);
Solution 1:[1]
You are using javascript. you can split it on "." then count the length of the array which should be 2 or less.
var x = '12.34.56';
var arr = x.split(".")
var number_of_dots = arr.length - 1
console.log(number_of_dots)
Solution 2:[2]
I think you can check if the number is Integer or float with this sample function
function isFloat(n){
return Number(n) === n && n % 1 !== 0;
}
function isInt(n) {
return n % 1 === 0;
}
and write catch, so if had error the entered number is not valid for you calculator
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | IT goldman |
| Solution 2 | farnaz farzipour |
