如何检查字符串是否为有效数字? [英] How can I check if a string is a valid number?

查看:50
本文介绍了如何检查字符串是否为有效数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在与旧的 VB6 IsNumeric() 函数相同的概念空间中有一些东西?

I'm hoping there's something in the same conceptual space as the old VB6 IsNumeric() function?

推荐答案

2020 年 10 月 2 日:请注意,许多基本方法都充满了细微的错误(例如空格、隐式部分解析、基数,数组强制等),这里的许多答案都没有考虑到.以下实现可能适合您,但请注意,它不适合小数点."以外的数字分隔符:

2nd October 2020: note that many bare-bones approaches are fraught with subtle bugs (eg. whitespace, implicit partial parsing, radix, coercion of arrays etc.) that many of the answers here fail to take into account. The following implementation might work for you, but note that it does not cater for number separators other than the decimal point ".":

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

示例

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

当然,如果需要,您可以否定这一点.例如,要实现您给出的 IsNumeric 示例:

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)
}

将包含数字的字符串转换为数字:

仅当字符串only 包含数字字符时才有效,否则返回NaN.

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

示例

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

将字符串松散地转换为数字

用于将 '12px' 转换为 12,例如:

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.

示例

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.

浮动

请记住,与 +num 不同,parseInt(顾名思义)会通过截去小数点后面的所有内容(如果你想使用 parseInt() 因为这种行为,你'可能最好使用另一种方法):

Floats

Bear in mind that, unlike +num, parseInt (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

空字符串

空字符串可能有点违反直觉.+num 将空字符串或带有空格的字符串转换为零,并且 isNaN() 假设相同:

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

但是parseInt()不同意:

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

这篇关于如何检查字符串是否为有效数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆