JavaScript Program to Check if a Number is Float or Integer

Posted in :

檢查字串是否為數字內容, 在 javascript 裡有很多方法:
https://stackoverflow.com/questions/175739/how-can-i-check-if-a-string-is-a-valid-number

function isNumeric(str) {
  if (typeof str != "string") return false // we only process strings!  
  return !isNaN(str) && // use type coercion to parse the _entirety_ of the string (`parseFloat` alone does not do this)...
         !isNaN(parseFloat(str)) // ...and ensure strings of whitespace fail
}

To check if a variable (including a string) is a number, check if it is not a number:

This works regardless of whether the variable content is a string or number.

isNaN(num)         // returns true if the variable does NOT contain a valid number

Examples

isNaN(123)         // false
isNaN('123')       // false
isNaN('1e10000')   // false (This translates to Infinity, which is a number)
isNaN('foo')       // true
isNaN('10px')      // true
isNaN('')          // false
isNaN(' ')         // false
isNaN(false)       // false

Of course, you can negate this if you need to. For example, to implement the IsNumeric example you gave:

function isNumeric(num){
  return !isNaN(num)
}

To convert a string containing a number into a number:

Only works if the string only contains numeric characters, else it returns NaN.

+num               // returns the numeric value of the string, or NaN 
                   // if the string isn't purely numeric characters

Examples

+'12'              // 12
+'12.'             // 12
+'12..'            // NaN
+'.12'             // 0.12
+'..12'            // NaN
+'foo'             // NaN
+'12px'            // NaN

To convert a string loosely to a number

Useful for converting ’12px’ to 12, for example:

parseInt(num)      // extracts a numeric value from the 
                   // start of the string, or NaN.

Examples

parseInt('12')     // 12
parseInt('aaa')    // NaN
parseInt('12px')   // 12
parseInt('foo2')   // NaN      These last three may
parseInt('12a5')   // 12       be different from what
parseInt('0x10')   // 16       you expected to see.

Floats

Bear in mind that, unlike +numparseInt (as the name suggests) will convert a float into an integer by chopping off everything following the decimal point (if you want to use parseInt() because of this behaviour, you’re probably better off using another method instead):

+'12.345'          // 12.345
parseInt(12.345)   // 12
parseInt('12.345') // 12

Empty strings

Empty strings may be a little counter-intuitive. +num converts empty strings or strings with spaces to zero, and isNaN() assumes the same:

+''                // 0
+'   '             // 0
isNaN('')          // false
isNaN('   ')       // false

But parseInt() does not agree:

parseInt('')       // NaN
parseInt('   ')    // NaN

Check if a value is a Float or an Integer in JavaScript
https://bobbyhadz.com/blog/javascript-check-if-value-is-float

檢查是否為浮點數:

// ✅ check if a value is a float
function isFloat(value) {
  if (
    typeof value === 'number' &&
    !Number.isNaN(value) &&
    !Number.isInteger(value)
  ) {
    return true;
  }

  return false;
}

console.log(isFloat(1)); // 👉️ false
console.log(isFloat(1.5)); // 👉️ true
console.log(isFloat(-1.5)); // 👉️ true
console.log(isFloat('1.5')); // 👉️ false

檢查是否為整數:

function isInteger(value) {
  return /^-?[0-9]+$/.test(value);
}

console.log(isInteger(12)); // 👉️ true
console.log(isInteger(-12)); // 👉️ true
console.log(isInteger(1.0)); // 👉️ true
console.log(isInteger('1')); // 👉️ true
console.log(isInteger(1.5)); // 👉️ false

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *