[英]I need to check if a number is not a decimal, I'm trying with Number.isInteger() but doesn't work
I want a prompt alert keep popping out until the user enters a number with no decimals, and for some reason, I don't understand Number.isInteger is not working, for example it accepts 4.4 or 8.333... Is it that Number.isInteger evaluate to true with decimals?我想要一个提示警报不断弹出,直到用户输入一个没有小数的数字,并且由于某种原因,我不明白 Number.isInteger 不起作用,例如它接受 4.4 或 8.333 ...是那个数字吗? isInteger 用小数计算为真?
check this snippet:检查这个片段:
function till() { let x = prompt("ingrese numero") if (Number.isInteger(parseInt(x)) === false) { till() } } till() console.log("FInally out!")
Your problem is parseInt
function just converts x to a Integer
thus even you enter 8.333 it is taken as 8.您的问题是
parseInt
function 只是将 x 转换为Integer
因此即使您输入 8.333 它也被视为 8。
Here is a simple solution to your problem!这是解决您问题的简单方法!
function till() { let x = prompt("ingrese numero") if (parseInt(x).= parseFloat(x)) { till() } } till() console.log("FInally out!")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.