为什么angular.isNumber()不能按预期工作? [英] Why is angular.isNumber() not working as expected?

查看:371
本文介绍了为什么angular.isNumber()不能按预期工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这看起来好像AngularJS的 angular.isNumber 不工作。它不与那些数字的字符串工作。难道我做错了什么?如果我只是用 isNaN()

It appears as if AngularJS's angular.isNumber is not working. It doesn't work with strings that are numbers. Am I doing something wrong? Should I just use isNaN()?

angular.isNumber('95.55') == false
angular.isNumber('95.55' * 1) == true
angular.isNumber('bla' * 1) == true
angular.isNumber(NaN) == true

我需要的东西,看看一个字符串是一个数字(当它实际上是)和 angular.isNumber()不让我做,除非我乘以1,但如果我这样做那么它将永远是正确的。此外 NaN的不是数字(根据定义),因此应该返回false。

I need something to see if a string is a number (when it actually is) and angular.isNumber() won't let me do that unless I multiply by 1, but if I do that then it will always be true. Also NaN is not a number (by definition) and so should return false.

推荐答案

的JavaScript 的typeof NaN的==='号'

如果您需要识别的字符串的作为的编号的,将其转换为的编号的,转换回的字符串和比较​​这对输入,例如。

If you need to recognise a String as a Number, cast it to Number, convert back to String and compare this against the input, for example.

function stringIsNumber(s) {
    var x = +s; // made cast obvious for demonstration
    return x.toString() === s;
}

stringIsNumber('95.55'); // true
stringIsNumber('foo'); // false
// still have
stringIsNumber('NaN'); // true

这篇关于为什么angular.isNumber()不能按预期工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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