我是一个JavaScript新手。
在python中,我可以像这样创建一个条件来检查一个数字是否是浮点数:
num = 1.5
if type(num) == float:
print('is a float')
我如何在JavaScript中做到这一点?有可能吗?
you can try:
let x=1.5;
if(!Number.isInteger(x)) {
console.log(`The number ${x} is a float`)
};
or
function checkFloat(x) {
//Check if the value is a number
if(typeof x == 'number' && !isNaN(x)) {
//Check if is integer
if(Number.isInteger(x)) {
//print the integer
console.log(`${x} is integer`);
}else {
//print the float number
console.log(`${x} is a float`);
};
}else {
//print the value that is not a number
console.log(`${x} is not a number`);
};
};