View Full Version : Detecting if Interager Has a Decimal
I'm working on a math program, but I am wondering how can I detect if it has a decimal?
var myNumber = 22.456566 // can be via user input, too
var numStr = myNumber.toString();
hasDecimal = (numStr.indexOf('.') != -1) ? true : false;
chrismiceli
11-08-2002, 11:55 PM
// where ever the math number is, i will use a form field in the example
test = document.forms[0].elements[0].value;
test.split(".")
if (test.length == 2) {
alert("has decimal");
}
else
alert("no decimal")
i think there is a way to do it with test.search(".") but i never have used the search function before.
Thanks. My problem was my variable wasn't a string. :rolleyes:
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.