PDA

View Full Version : Detecting if Interager Has a Decimal


Carl
11-08-2002, 11:46 PM
I'm working on a math program, but I am wondering how can I detect if it has a decimal?

kdj2
11-08-2002, 11:54 PM
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.

Carl
11-09-2002, 05:27 PM
Thanks. My problem was my variable wasn't a string. :rolleyes: