简体   繁体   中英

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?

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.

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!")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM