function isInteger(n) {return ((typeof n==='number')&&(n%1===0));}
function isFloat(n) {return ((typeof n==='number')&&(n%1!==0));}
function isNumber(n) {return (typeof n==='number');}
在java脚本中,所有数字都是internally 64 bit floating point,与java中的双精度相同。javascript中没有不同的类型,所有类型都由类型number表示。因此,您将无法进行instanceof检查。但是,您可以使用上面给出的解决方案来确定它是否是小数。java脚本的设计者认为使用单一类型可以避免许多类型转换错误。
var a = 99999999999999999999;var b = 999999999999999999999; // just one more 9 will kill the show!
var aIsInteger = ( a===parseInt(a) )?"a is ok":"a fails";var bIsInteger = ( b===parseInt(b) )?"b is ok":"b fails";
alert(aIsInteger+"; "+bIsInteger);
function isInt(quale) {var valore = $('#'+quale).val().toLowerCase();if (isNaN(Number(String(valore))) || (valore.indexOf("e") > 0)) {// Not int} else {// Is Int!}}
还有这个:
function isFloat(quale) {var valore = $('#'+quale).val();valore = valore.replace(",", "");if (isNaN(String(valore)) || (valore.indexOf("e") > 0)) {// Not Float} else {// Float}}
function IsInteger(iVal) {var iParsedVal; //our internal converted int value
iParsedVal = parseInt(iVal,10);
if (isNaN(iParsedVal) || Infinity == iParsedVal || -Infinity == iParsedVal) //sanity check - guard against empty strings and max/min valuesreturn false;elsereturn Number(iVal) === (iParsedVal | 0); //the 2nd operand group (intValue | 0), evaluates to true only if the intValue is an integer; so an int type will only return true}
function IsFloat(fVal) {var fParsedVal; //our internal converted float value
fParsedVal = parseFloat(fVal);
if (isNaN(fParsedVal) || Infinity == fParsedVal || -Infinity == fParsedVal) //sanity check - guard against empty strings and max/min valuesreturn false;elsereturn !!(fVal % 1); //true only if there is a fractional value after the mod op; the !! returns the opposite value of the op which reflects the function's return value}
function int(a) {return a - a === 0 && a.toString(32).indexOf('.') === -1}
function float(a) {return a - a === 0 && a.toString(32).indexOf('.') !== -1}
<html><body><form method="post" action="#"><input type="text" id="number_id"/><input type="submit" value="send"/></form><p id="message"></p><script>var flt=document.getElementById("number_id").value;if(isNaN(flt)==false && Number.isInteger(flt)==false){document.getElementById("message").innerHTML="the number_id is a float ";}else{document.getElementById("message").innerHTML="the number_id is a Integer";}</script></body></html>