0

im trying to check if a number is a whole after a calculation. What I have so far prints out how many times one number gets divided by another, but when the number is not whole it dose not print anything out. Heres my code:

function round() {
    var percent = document.getElementById('percent_sale').value;
    var perShare = document.getElementById('singleShare').value;

    var result = (percent / perShare);
    if(result % 1 == 0) {
        document.getElementById('results1').innerHTML = ('Number of shares:'+result);

    } else {
        document.getElementById(results1).innerHTML = ('number of shares must ');

    }
}

The values get input buy a user, and the percent for sale is say 50 and the single share is say 2.5 this would return 20 shares.

What I need is if I put in something like 50 for sale and 3.15 single share it tells the user to make equal number of shares as it would return 15.87

Any ideas where ive gone wrong?

Adam
  • 455
  • 1
  • 3
  • 18

1 Answers1

0

Convert your number into string and then check if the string contains only numbers

var num = 15;
var n = num.toString();

This will convert it into string then this

String.prototype.isNumber = function(){return /^\d+$/.test(this);}
console.log("123123".isNumber()); // outputs true
console.log("+12".isNumber()); // outputs false

For further reference.Link StackOverFlow

Community
  • 1
  • 1
Areeb Gillani
  • 440
  • 4
  • 25
  • One should attempt to avoid mutating the base prototypes in general practice. It is bad form. Also a regexp is unnecessary use this: `function isNumber(n) { return Number(n) === n; };` – Sukima May 24 '15 at 13:36
  • For that reason I gave a link. On the other end it does what is asked. I am just being a problem solver nothing else :-) – Areeb Gillani May 24 '15 at 13:37
  • Not trying to be rude but just being a problem solver vs leading by example is the difference between mediocre and excellence. – Sukima May 24 '15 at 13:39