3

After doing a sqrt()

How can I be check to see if the result contains only whole numbers or not?

I was thinking Regex to check for a decimal - if it contains a decimal, that means it didn't root evenly into whole numbers. Which would be enough info for me.

but this code isnt working...

result = sqrt(stringContainingANumber);
decimal = new RegExp(".");
document.write(decimal.test(result)); 

I bet there's other ways to accomplish the same thing though.

monkey blot
  • 985
  • 3
  • 10
  • 18
  • Possible duplicate of [Check if a number has a decimal place/is a whole number](http://stackoverflow.com/questions/2304052/check-if-a-number-has-a-decimal-place-is-a-whole-number) – Benny Bottema Apr 14 '16 at 10:01

7 Answers7

7

. means any char. You have to quote the dot. "\."

Or you could test

if (result > Math.floor(result)) {
   // not an decimal
}
stefan bachert
  • 9,413
  • 4
  • 33
  • 40
5

You can use the % operator:

result % 1 === 0;  // rest after dividing by 1 should be 0 for whole numbers
pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • But, I'm assuming that `result` is the already a number here as it's the result of the `sqrt()` function. I'm not sure you're solving what the OP asked. – jfriend00 Mar 23 '12 at 12:10
  • @jfriend00: Good catch. Then it's even more straight-forward to use the `%` operator I guess. – pimvdb Mar 23 '12 at 12:13
4

Use indexOf():

​var myStr = "1.0";
myStr.indexOf("."); // Returns 1

// Other examples
myStr.indexOf("1"); // Returns 0 (meaning that "1" may be found at index 0)
myStr.indexOf("2"); // Returns -1 (meaning can't be found)
James Hill
  • 60,353
  • 20
  • 145
  • 161
  • 1
    +1 for using `.indexOf()` instead of a regex since it's simpler and faster than a regex for this particular problem. – jfriend00 Mar 23 '12 at 12:08
1

"." has meaning in the regex syntax which is "anything" you need to escape it using "\."

John Sobolewski
  • 4,512
  • 1
  • 20
  • 26
0

If its a string we can just use split function and then check the length of the array returned. If its more than 1 it has decimal point else not :). This doesn't work for numbers though :(. Please see the last edit. It works for string as well now :)

function checkDecimal() {
    var str = "202.0";
    var res = str.split(".");
    alert(res.length >1);
    var str1 = "20";

    alert(str1.split(".").length>1);
 }

Hope it helps someone. Happy Learning :)

Vatsal
  • 2,068
  • 4
  • 21
  • 24
0

Are you looking for checking string containing decimal digits , you can try like this

var num = "123.677";
if (!isNaN(Number(num)) {
alert("decimal no");
}
else {
alert("Not a decimal number");
}
0

I am sorry. This answer is too late. but I hope this will help.

function isThisDecimal(val){
  if (!(val.indexOf(".") == -1)){
    return true; // decimal 
  }
  return false; // number
}

console.log(isThisDecimal("12.00")); //true
console.log(isThisDecimal("12.12")); //true
console.log(isThisDecimal("12"));// false
suba
  • 1,320
  • 15
  • 22