[英]Checking if user input is a floating number
choice = input.questionFloat('\t1. Display all members\' information \n\t2. Display member information \n\t3. Add new member \n\t4. Update points earned \n\t5. Statistics\n\t6. Exit \n\t>> ');
if (choice.toString().includes('.')) {
console.log ('Please enter a valid input.');
}
選擇包含來自用戶的輸入。 如果choice是一個浮點數,它會提示用戶這是一個無效的輸入。 如果有更好的方法來代替使用 .includes?
您可以使用 JavaScript 輕松做到這一點。
首先取並轉換為數字並檢查它是否為真值(非 NaN)並檢查它是否不是整數。
const num = Number('123.3')
if (num && !Number.isInteger(num)){
//float
}
為什么不將其與 int 中的值進行比較?
const intValue = 12
console.log(parseInt(value) !== value) // false => is an int
const floatValue = 12.1
console.log(parseInt(value) !== value) // true => is not an int
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.