如何使用Javascript檢查數字float浮點數還是int整數

6 月 19, 2019 | Javascript, | 0 comments

isFloat and isInteger in javascript

解決的方法可以使用下列函數來檢查上isFloat和isInteger

function isFloat(n) {
  return n === +n && n !== (n|0);
}

function isInteger(n) {
  return n === +n && n === (n|0);
}

Demo

See the Pen How do check that a number is a float or integer in Javascript by Leon Cheng (@jq153387) on CodePen.