Reputation: 7551
If use %
then it works for integer%interger:
10%5 == 0 return true;
10%3 != 0 return false;
But how to use code to check whether variable a can
a%0.002 == 0 return true;
a%0.002 != 0 return false;
a could be integer or float.
Thanks in advance for any hint
Upvotes: 3
Views: 4355
Reputation: 104760
You can use the modulo operator to verify the division results in an integer:
var n=25, dec=.0125;
(n/dec)%1==0; // returned value: (Boolean) true
var n=25, dec=.022;
(n/dec)%1==0; // returned value: (Boolean) false
Upvotes: 7
Reputation: 388316
I think you need some custom function like
function test(a, b){
var result = a / b;
return Math.round(result) == result;
}
Upvotes: 3
Reputation: 55720
First, divisibility is defined only for integers. So your statement is not necessarily mathematically correct.
Now, if you just want to see if a number can be expressed as an integer multiple of another decimal number, than the best way do do that is perhaps to implement a function that checks whether the result of the division is an integer.
In general, you can think of the %
(modulo operator) as performing an integer division, and returning the remainder.
Upvotes: 7