我希望在与旧的VB6 IsNumeric()函数相同的概念空间中有什么东西?
当前回答
我已经测试过了,迈克尔的解决方案是最好的。投票支持上面的答案(在这个页面上搜索“如果你真的想确保有一个字符串”来找到它)。本质上,他的答案是:
function isNumeric(num){
num = "" + num; //coerce num to be a string
return !isNaN(num) && !isNaN(parseFloat(num));
}
它适用于每个测试用例,我在这里记录了这些测试用例:https://jsfiddle.net/wggehvp9/5/
许多其他解决方案在这些边缘情况下失败:“”、null、“”、true和[]。理论上,您可以使用它们,并进行适当的错误处理,例如:
return !isNaN(num);
or
return (+num === +num);
具有特殊处理/\s/,null,“”,true,false,[](和其他?)
其他回答
也许有一两个人遇到这个问题,他们需要比平时更严格的检查(就像我一样)。在这种情况下,这可能很有用:
if(str === String(Number(str))) {
// it's a "perfectly formatted" number
}
当心这将拒绝类似于.1、40.000、080、00.1的字符串。它非常挑剔——字符串必须匹配数字的“最最小完美形式”才能通过测试。
它使用String和Number构造函数将字符串转换为一个数字,然后再次返回,从而检查JavaScript引擎的“完美最小形式”(用初始Number构造函数转换为的形式)是否与原始字符串匹配。
如果要查找正数(例如门牌号),请使用:
if (mystring > 0) ...
我使用的是:
const isNumber = s => !isNaN(+s)
在将参数传递给其构造函数时,可以使用Number的结果。
如果参数(字符串)不能转换为数字,它将返回NaN,因此您可以确定提供的字符串是否为有效数字。
注意:当传递空字符串或'\t\t'和'\n\t'作为Number时,将返回0;传递true将返回1,传递false将返回0。
Number('34.00') // 34
Number('-34') // -34
Number('123e5') // 12300000
Number('123e-5') // 0.00123
Number('999999999999') // 999999999999
Number('9999999999999999') // 10000000000000000 (integer accuracy up to 15 digit)
Number('0xFF') // 255
Number('Infinity') // Infinity
Number('34px') // NaN
Number('xyz') // NaN
Number('true') // NaN
Number('false') // NaN
// cavets
Number(' ') // 0
Number('\t\t') // 0
Number('\n\t') // 0
2019:包括ES3、ES6和TypeScript示例
也许这已经被重复了太多次了,但是我今天也和这一个进行了斗争,并想发布我的答案,因为我没有看到任何其他答案能如此简单或彻底地做到这一点:
ES3
var isNumeric = function(num){
return (typeof(num) === 'number' || typeof(num) === "string" && num.trim() !== '') && !isNaN(num);
}
ES6
const isNumeric = (num) => (typeof(num) === 'number' || typeof(num) === "string" && num.trim() !== '') && !isNaN(num);
字体
const isNumeric = (num: any) => (typeof(num) === 'number' || typeof(num) === "string" && num.trim() !== '') && !isNaN(num as number);
这似乎很简单,涵盖了我在许多其他帖子中看到的所有基础,并自己思考:
// Positive Cases
console.log(0, isNumeric(0) === true);
console.log(1, isNumeric(1) === true);
console.log(1234567890, isNumeric(1234567890) === true);
console.log('1234567890', isNumeric('1234567890') === true);
console.log('0', isNumeric('0') === true);
console.log('1', isNumeric('1') === true);
console.log('1.1', isNumeric('1.1') === true);
console.log('-1', isNumeric('-1') === true);
console.log('-1.2354', isNumeric('-1.2354') === true);
console.log('-1234567890', isNumeric('-1234567890') === true);
console.log(-1, isNumeric(-1) === true);
console.log(-32.1, isNumeric(-32.1) === true);
console.log('0x1', isNumeric('0x1') === true); // Valid number in hex
// Negative Cases
console.log(true, isNumeric(true) === false);
console.log(false, isNumeric(false) === false);
console.log('1..1', isNumeric('1..1') === false);
console.log('1,1', isNumeric('1,1') === false);
console.log('-32.1.12', isNumeric('-32.1.12') === false);
console.log('[blank]', isNumeric('') === false);
console.log('[spaces]', isNumeric(' ') === false);
console.log('null', isNumeric(null) === false);
console.log('undefined', isNumeric(undefined) === false);
console.log([], isNumeric([]) === false);
console.log('NaN', isNumeric(NaN) === false);
您还可以尝试自己的isNumeric函数,并在这些用例中刚刚过去,然后扫描所有用例的“true”。
或者,查看每个返回的值:
推荐文章
- 在React Native中使用Fetch授权头
- 为什么我的球(物体)没有缩小/消失?
- 如何使用jQuery检测页面的滚动位置
- if(key in object)或者if(object. hasownproperty (key)
- 一元加/数字(x)和parseFloat(x)之间的区别是什么?
- angularjs中的compile函数和link函数有什么区别
- 删除绑定中添加的事件监听器
- 很好的初学者教程socket.io?
- HtmlSpecialChars在JavaScript中等价于什么?
- React: 'Redirect'没有从' React -router-dom'中导出
- 如何在React中使用钩子强制组件重新渲染?
- 我如何使用Jest模拟JavaScript的“窗口”对象?
- 我如何等待一个承诺完成之前返回一个函数的变量?
- 在JavaScript中根据键值查找和删除数组中的对象
- 使嵌套JavaScript对象平放/不平放的最快方法