为什么 isNaN(" ") (带空格的字符串)等于 false?

发布于 2024-07-19 15:52:30 字数 297 浏览 11 评论 0 原文

在 JavaScript 中,为什么 isNaN(" ") 计算结果为 false,而 isNaN(" x") 计算结果为 true

我正在对文本输入字段执行数字运算,并检查该字段是否为 null""NaN。 当有人在字段中输入少量空格时,我的验证在所有三个空格上都会失败,并且我很困惑为什么它会通过 isNaN 检查。

In JavaScript, why does isNaN(" ") evaluate to false, but isNaN(" x") evaluate to true?

I’m performing numerical operations on a text input field, and I’m checking if the field is null, "", or NaN. When someone types a handful of spaces into the field, my validation fails on all three, and I’m confused as to why it gets past the isNaN check.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(25

栖迟 2024-07-26 15:52:30

JavaScript 将空字符串解释为 0,从而导致 isNAN 测试失败。 您可以先对字符串使用 parseInt,这不会将空字符串转换为 0。结果应该会失败 isNAN。

JavaScript interprets an empty string as a 0, which then fails the isNAN test. You can use parseInt on the string first which won't convert the empty string to 0. The result should then fail isNAN.

花开半夏魅人心 2024-07-26 15:52:30

您可能会感到惊讶,也可能不会,但这里有一些测试代码,可以向您展示 JavaScript 引擎的古怪之处。

document.write(isNaN("")) // false
document.write(isNaN(" "))  // false
document.write(isNaN(0))  // false
document.write(isNaN(null)) // false
document.write(isNaN(false))  // false
document.write("" == false)  // true
document.write("" == 0)  // true
document.write(" " == 0)  // true
document.write(" " == false)  // true
document.write(0 == false) // true
document.write(" " == "") // false

所以这意味着

" " == 0 == false

玩得

"" == 0 == false

,但是

"" != " "

开心:)

You may find this surprising or maybe not, but here is some test code to show you the wackyness of the JavaScript engine.

document.write(isNaN("")) // false
document.write(isNaN(" "))  // false
document.write(isNaN(0))  // false
document.write(isNaN(null)) // false
document.write(isNaN(false))  // false
document.write("" == false)  // true
document.write("" == 0)  // true
document.write(" " == 0)  // true
document.write(" " == false)  // true
document.write(0 == false) // true
document.write(" " == "") // false

so this means that

" " == 0 == false

and

"" == 0 == false

but

"" != " "

Have fun :)

薆情海 2024-07-26 15:52:30

为了更好地理解它,请打开 Ecma-Script 规范 pdf第 43 页“ToNumber 应用于字符串类型”

如果字符串具有数字语法,可以包含任意数量的空白字符,则可以将其转换为 Number 类型。 空字符串的计算结果为 0。字符串 'Infinity' 也应该给出

isNaN('Infinity'); // false

To understand it better, please open Ecma-Script spec pdf on page 43 "ToNumber Applied to the String Type"

if a string has a numerical syntax, which can contain any number of white-space characters, it can be converted to Number type. Empty string evaluates to 0. Also the string 'Infinity' should give

isNaN('Infinity'); // false
邮友 2024-07-26 15:52:30

尝试使用:

alert(isNaN(parseInt("   ")));

或者

alert(isNaN(parseFloat("    ")));

Try using:

alert(isNaN(parseInt("   ")));

Or

alert(isNaN(parseFloat("    ")));
尸血腥色 2024-07-26 15:52:30

来自 MDN 原因对于您面临的问题

当 isNaN 函数的参数不是 Number 类型时,该值首先被强制转换为 Number。 然后测试结果值以确定它是否为 NaN。

您可能需要检查以下综合答案,其中还涵盖了 NaN 相等性比较。

如何测试 JavaScript 变量是否为 NaN

From MDN reason for the issue you are facing

When the argument to the isNaN function is not of type Number, the value is first coerced to a Number. The resulting value is then tested to determine whether it is NaN.

You may want to check the following comprehensive answer which covers the NaN comparison for equality as well.

How to test if a JavaScript variable is NaN

别低头,皇冠会掉 2024-07-26 15:52:30

我认为这是因为 Javascript 的输入:' ' 被转换为零,而 'x' 则不是:

alert(' ' * 1); // 0
alert('x' * 1); // NaN

I think it's because of Javascript's typing: ' ' is converted to zero, whereas 'x' isn't:

alert(' ' * 1); // 0
alert('x' * 1); // NaN
虐人心 2024-07-26 15:52:30

不完全正确的答案

Antonio Haley 的高度赞成并接受的答案在这里做出了一个错误的假设,即这个过程通过 JavaScript 的 <代码>parseInt函数:

您可以在字符串上使用 parseInt ...结果应该失败 isNAN。

我们可以很容易地用字符串 "123abc" 反驳这个说法:

parseInt("123abc")    // 123     (a number...
isNaN("123abc")       // true     ...which is not a number)

通过这个,我们可以看到 JavaScript 的 parseInt 函数返回 "123abc" 作为数字 123,但其 isNaN 函数告诉我们 "123abc" 不是 一个数字。

正确答案

ECMAScript-262 定义了 isNaN 检查在 第 18.2.3 节

18.2.3 isNaN(数字)

isNaN 函数是 %isNaN% 内部对象。 当使用一个参数编号调用 isNaN 函数时,将执行以下步骤:

  1. num 为? ToNumber(数字)
  2. 如果 numNaN,则返回 true
  3. 否则,返回false

它引用的 ToNumber 函数也在 ECMAScript-262 的第 7.1 节中定义.3。 在这里,我们了解 JavaScript 如何处理传入此函数的字符串。

问题中给出的第一个示例是一个仅包含空格字符的字符串。 本节规定:

空或仅包含空格的 StringNumericLiteral 将转换为 +0

因此," " 示例字符串被转换为 +0,这是一个数字。

同一节还指出:

如果语法无法将 String 解释为 StringNumericLiteral 的扩展,则 ToNumber 的结果为 NaN >.

如果不引用该部分中包含的所有检查,问题中给出的 " x" 示例就会属于上述情况,因为它不能解释为 StringNumericLiteral。 因此," x" 被转换为 NaN

The Not-Entirely-Correct Answer

Antonio Haley's highly upvoted and accepted answer here makes a wrong assumption that this process goes through JavaScript's parseInt function:

You can use parseInt on the string ... The result should then fail isNAN.

We can easily disprove this statement with the string "123abc":

parseInt("123abc")    // 123     (a number...
isNaN("123abc")       // true     ...which is not a number)

With this, we can see that JavaScript's parseInt function returns "123abc" as the number 123, yet its isNaN function tells us that "123abc" isn't a number.

The Correct Answer

ECMAScript-262 defines how the isNaN check works in section 18.2.3.

18.2.3 isNaN (Number)

The isNaN function is the %isNaN% intrinsic object. When the isNaN function is called with one argument number, the following steps are taken:

  1. Let num be ? ToNumber(number).
  2. If num is NaN, return true.
  3. Otherwise, return false.

The ToNumber function it references is also defined in ECMAScript-262's section 7.1.3. In here, we get told how JavaScript handles Strings which are passed in to this function.

The first example given in the question is a string containing nothing but white space characters. This section states that:

A StringNumericLiteral that is empty or contains only white space is converted to +0.

The " " example string is therefore converted to +0, which is a number.

The same section also states:

If the grammar cannot interpret the String as an expansion of StringNumericLiteral, then the result of ToNumber is NaN.

Without quoting all of the checks contained within that section, the " x" example given in the question falls into the above condition as it cannot be interpreted as a StringNumericLiteral. " x" is therefore converted to NaN.

慈悲佛祖 2024-07-26 15:52:30

函数 isNaN("") 执行字符串到数字类型的强制转换

ECMAScript 3-5 为 typeof 运算符定义了以下返回值:

  • 未定义
  • 对象(null、对象、数组)
  • 布尔
  • 字符串
  • 函数

最好将我们的测试包装在函数体中:

function isNumber (s) {
    return typeof s == 'number'? true
           : typeof s == 'string'? (s.trim() === ''? false : !isNaN(s))
           : (typeof s).match(/object|function/)? false
           : !isNaN(s)
}

此函数并非旨在测试变量类型,而是测试强制值。 您可能希望将此函数调用为 isNumberCoerced()

例如,布尔值和字符串被强制转换为数字,因此如果不需要测试除 类型 以外的类型, >字符串和数字,那么以下代码片段可以用作某些条件的一部分:

if (!isNaN(s) && s.toString().trim()!='') // 's' can be boolean, number or string
    alert("s is a number")

The function isNaN("") performs a String to Number type coercion

ECMAScript 3-5 defines the following return values for the typeof operator:

  • undefined
  • object (null, objects, arrays)
  • boolean
  • number
  • string
  • function

Better to wrap our test in a function body:

function isNumber (s) {
    return typeof s == 'number'? true
           : typeof s == 'string'? (s.trim() === ''? false : !isNaN(s))
           : (typeof s).match(/object|function/)? false
           : !isNaN(s)
}

This function is not intented to test variable type, instead it tests the coerced value. For instance, booleans and strings are coerced to numbers, so perhaps you may want to call this function as isNumberCoerced()

if there's no need to test for types other than string and number, then the following snippet might be used as part of some condition:

if (!isNaN(s) && s.toString().trim()!='') // 's' can be boolean, number or string
    alert("s is a number")
不忘初心 2024-07-26 15:52:30

如果您想实现精确的 isNumber 函数,可以使用 Douglas Crockford 的 Javascript:The Good Parts [第 105 页] 中的一种方法。

var isNumber = function isNumber(value) {
   return typeof value === 'number' && 
   isFinite(value);
}

If you would like to implement an accurate isNumber function, here is one way to do it from Javascript: The Good Parts by Douglas Crockford [page 105]

var isNumber = function isNumber(value) {
   return typeof value === 'number' && 
   isFinite(value);
}
一身骄傲 2024-07-26 15:52:30

NaN !== "not a number"

NaN 是 Number 类型的值,

这是 ECMAScript 中 isNaN() 的定义

1. Let num be ToNumber(number).
2. ReturnIfAbrupt(num).
3. If num is NaN, return true.
4. Otherwise, return false.

尝试将任何值转换为 Number。

Number(" ") // 0
Number("x") // NaN
Number(null) // 0

如果你想判断该值是否为NaN,你应该先尝试将其转换为Number值。

NaN !== "not a number"

NaN is a value of Number Type

this is a definition of isNaN() in ECMAScript

1. Let num be ToNumber(number).
2. ReturnIfAbrupt(num).
3. If num is NaN, return true.
4. Otherwise, return false.

Try to convert any value to Number.

Number(" ") // 0
Number("x") // NaN
Number(null) // 0

If you want to determine if the value is NaN, you should try to convert it to a Number value firstly.

雅心素梦 2024-07-26 15:52:30

如果您确实想要正确检查它是否是整数,我建议您使用以下函数:

function isInteger(s)
{
   return Math.ceil(s) == Math.floor(s);
}

I suggest you to use the following function if you really want a proper check if it is an integer:

function isInteger(s)
{
   return Math.ceil(s) == Math.floor(s);
}
心碎无痕… 2024-07-26 15:52:30

isNaN(" ") 为 false 是 isNaN 全局函数令人困惑的行为的一部分,因为它将非数字强制转换为数字类型。

来自 MDN

自从 isNaN 函数规范的最早版本以来,它对非数字参数的行为一直令人困惑。 当 isNaN 函数的参数不是 Number 类型时,该值首先被强制转换为 Number。 然后测试结果值以确定它是否为 NaN。 因此,对于非数字,当强制转换为数字类型时会产生有效的非 NaN 数值(特别是空字符串和布尔基元,当强制转换时给出数值零或一),“假”返回值可能是意外的; 例如,空字符串肯定是“不是数字”。

另请注意,在 ECMAScript 6 中,现在还提供了 Number.isNaN 方法,根据 MDN:

与全局 isNaN() 函数相比,Number.isNaN() 不会遇到将参数强制转换为数字的问题。 这意味着现在可以安全地传递通常会转换为 NaN 的值,但实际上与 NaN 的值不同。 这也意味着只有数字类型的值(也是 NaN)才会返回 true

不幸的是

即使是 ECMAScript 6 Number.isNaN 方法也有其自身的问题,如博客文章中所述 - 修复丑陋的 JavaScript 和 ES6 NaN 问题

That isNaN(" ") is false is part of the confusing behavior of the isNaN global function due to its coercion of non-numbers to a numeric type.

From MDN:

Since the very earliest versions of the isNaN function specification, its behavior for non-numeric arguments has been confusing. When the argument to the isNaN function is not of type Number, the value is first coerced to a Number. The resulting value is then tested to determine whether it is NaN. Thus for non-numbers that when coerced to numeric type result in a valid non-NaN numeric value (notably the empty string and boolean primitives, which when coerced give numeric values zero or one), the "false" returned value may be unexpected; the empty string, for example, is surely "not a number."

Note also that with ECMAScript 6, there is also now the Number.isNaN method, which according to MDN:

In comparison to the global isNaN() function, Number.isNaN() doesn't suffer the problem of forcefully converting the parameter to a number. This means it is now safe to pass values that would normally convert to NaN, but aren't actually the same value as NaN. This also means that only values of the type number, that are also NaN, return true.

Unfortunately:

Even the ECMAScript 6 Number.isNaN method has its own issues, as outlined in the blog post - Fixing the ugly JavaScript and ES6 NaN problem.

日久见人心 2024-07-26 15:52:30

isNaN 函数需要一个 Number 作为其参数,因此任何其他类型的参数(在您的情况下为字符串)将在执行实际函数逻辑之前转换为 Number。 (请注意 NaN 也是 Number 类型的值!)

顺便说一句。 这对于所有内置函数来说很常见 - 如果它们需要某种类型的参数,则实际参数将使用标准转换函数进行转换。 所有基本类型(bool、string、number、object、date、null、undefined)之间都有标准转换。

StringNumber 的标准转换可以显式调用与Number()。 所以我们可以看到:

  • Number(" ") 计算结果为 0
  • Number(" x") 计算结果为 NaN >

鉴于此,isNaN 函数的结果是完全符合逻辑的!

真正的问题是为什么标准字符串到数字的转换会像这样工作。 字符串到数字的转换实际上旨在将“123”或“17.5e4”等数字字符串转换为等效的数字。 转换首先跳过初始空格(因此“123”有效),然后尝试将其余部分解析为数字。 如果它不可解析为数字(“x”则不可解析),则结果为 NaN。 但是有一个明确的特殊规则,即空字符串或只有空格的字符串将转换为 0。因此这解释了转换。

参考:http://www.ecma-international.org/ ecma-262/5.1/#sec-9.3.1

The isNaN function expects a Number as its argument, so arguments of any other type (in your case a string) will be converted to Number before the actual function logic is performed. (Be aware that NaN is also a value of type Number!)

Btw. this is common for all built-in functions - if they expect an argument of a certain type, the actual argument will be converted using the standard conversion functions. There are standard conversions between all the basic types (bool, string, number, object, date, null, undefined.)

The standard conversion for String to Number can be invoked explicit with Number(). So we can see that:

  • Number(" ") evaluates to 0
  • Number(" x") evaluates to NaN

Given this, the result of the isNaN function is completely logical!

The real question is why the standard String-to-Number conversion works like it does. The string-to-number conversion is really intended to convert numeric strings like "123" or "17.5e4" to the equivalent numbers. The conversion first skips initial whitespace (so " 123" is valid) and then tries to parse the rests as a number. If it is not parseable as a number ("x" isn't) then the result is NaN. But there is the explicit special rule that a string which is empty or only whitespace is converted to 0. So this explains the conversion.

Reference: http://www.ecma-international.org/ecma-262/5.1/#sec-9.3.1

旧城空念 2024-07-26 15:52:30

我不确定为什么,但为了解决这个问题,您可以在检查之前删除空格。 无论如何你可能都想这样做。

I'm not sure why, but to get around the problem you could always trim whitespace before checking. You probably want to do that anyway.

盛夏尉蓝 2024-07-26 15:52:30

我编写了这个快速的小函数来帮助解决这个问题。

function isNumber(val) {
     return (val != undefined && val != null && val.toString().length > 0 && val.toString().match(/[^0-9\.\-]/g) == null);
};

它只是检查非数字 (0-9)、非“-”或“.”以及非未定义、null 或空的任何字符,如果没有匹配则返回 true。 :)

I wrote this quick little function to help solve this problem.

function isNumber(val) {
     return (val != undefined && val != null && val.toString().length > 0 && val.toString().match(/[^0-9\.\-]/g) == null);
};

It simply checks for any characters that aren't numeric (0-9), that aren't '-' or '.', and that aren't undefined, null or empty and returns true if there's no matches. :)

瞄了个咪的 2024-07-26 15:52:30

正如其他人所解释的,isNaN 函数会在验证空字符串之前将其强制转换为数字,从而将空字符串更改为 0(这是一个有效的数字)。
但是,我发现当尝试解析空字符串或仅包含空格的字符串时,parseInt 函数将返回 NaN。 因此,以下组合似乎运行良好:

if ( isNaN(string) || isNaN(parseInt(string)) ) console.log('Not a number!');

此检查将起作用对于正数、负数和带小数点的数字,所以我相信它涵盖了所有常见的数字情况。

As other explained the isNaN function will coerce the empty string into a number before validating it, thus changing an empty string into 0 (which is a valid number).
However, I found that the parseInt function will return NaN when trying to parse an empty string or a string with only spaces. As such the following combination seems to be working well:

if ( isNaN(string) || isNaN(parseInt(string)) ) console.log('Not a number!');

This check will work for positive numbers, negative numbers and numbers with a decimal point, so I believe it covers all common numerical cases.

疧_╮線 2024-07-26 15:52:30

我用这个

    function isNotANumeric(val) {
    	if(val.trim && val.trim() == "") {
         return true;
      } else {
      	 return isNaN(parseFloat(val * 1));
      }
    }
    
    alert(isNotANumeric("100"));  // false
    alert(isNotANumeric("1a"));   // true
    alert(isNotANumeric(""));     // true
    alert(isNotANumeric("   "));  // true

I use this

    function isNotANumeric(val) {
    	if(val.trim && val.trim() == "") {
         return true;
      } else {
      	 return isNaN(parseFloat(val * 1));
      }
    }
    
    alert(isNotANumeric("100"));  // false
    alert(isNotANumeric("1a"));   // true
    alert(isNotANumeric(""));     // true
    alert(isNotANumeric("   "));  // true

路弥 2024-07-26 15:52:30

这个功能在我的测试中似乎有效

function isNumber(s) {
    if (s === "" || s === null) {
        return false;
    } else {
        var number = parseInt(s);
        if (number == 'NaN') {
            return false;
        } else {
            return true;
        }
    }
}

This function seemed to work in my tests

function isNumber(s) {
    if (s === "" || s === null) {
        return false;
    } else {
        var number = parseInt(s);
        if (number == 'NaN') {
            return false;
        } else {
            return true;
        }
    }
}
蒲公英的约定 2024-07-26 15:52:30

关于什么

function isNumberRegex(value) {        
    var pattern = /^[-+]?\d*\.?\d*$/i;
    var match = value.match(pattern);
    return value.length > 0 && match != null;
}

What about

function isNumberRegex(value) {        
    var pattern = /^[-+]?\d*\.?\d*$/i;
    var match = value.match(pattern);
    return value.length > 0 && match != null;
}
爱格式化 2024-07-26 15:52:30

JavaScript 内置 isNaN 函数 - 正如默认情况下所预期的 - 一个“动态类型运算符”。
因此,(在 DTC 过程中)可能产生简单 true | 的所有值。 false 例如 ""、" "、" 000",不能为 NaN。

这意味着提供的参数将首先进行转换,如下所示:

function isNaNDemo(arg){
   var x = new Number(arg).valueOf();
   return x != x;
}

解释:

在函数体的顶行中,我们(首先)尝试成功地将参数转换为数字对象。 (第二),为了方便起见,我们使用点运算符立即剥离所创建对象的原始值。

在第二行中,我们采用上一步中获得的值,并且利用 NaN 不等于宇宙中任何东西的事实,甚至不等于本身,例如: NaN == NaN >>> false 最终将其与自身进行比较(是否不平等)。

这样,仅当且仅当提供的参数返回尝试转换为数字对象失败时,函数返回才会产生 true,即,不是-a-number; 例如,NaN。


isNaNstatic( )

但是,对于静态类型运算符 - 如果需要并且在需要时 - 我们可以编写一个简单得多的函数,例如:

function isNaNstatic(x){   
   return x != x;
}

并完全避免 DTC,这样如果参数不是明确的 NaN 数,它将返回 false。 因此,针对以下内容进行测试:

isNaNStatic(" x"); // 将返回 false 因为它仍然是一个字符串。

然而:
isNaNStatic(1/"x"); // 当然会返回 true。 例如 isNaNStatic(NaN); >>> 正确。

但与 isNaN 相反,isNaNStatic("NaN"); >>> false 因为它(参数)是一个普通字符串。

附:
isNaN 的静态版本在现代编码场景中非常有用。 这很可能是我花时间发布此内容的主要原因之一。

问候。

The JavaScript built-in isNaN function, is - as should be expected by default - a "Dynamic Type Operator".
Therefore all values which (during the DTC process) may yield a simple true | false such as "", " ", " 000", cannot be NaN.

Meaning that the argument supplied will first undergo a conversion as in:

function isNaNDemo(arg){
   var x = new Number(arg).valueOf();
   return x != x;
}

Explanation:

In the top line of the function body, we are (first) trying to successfully convert the argument into a number object. And (second), using the dot operator we are - for our own convenience - immediately stripping off, the primitive value of the created object.

In the second line, we are taking the value obtained in the previous step, and the advantage of the fact that NaN is not equal to anything in the universe, not even to itself, e.g.: NaN == NaN >> false to finally compare it (for inequality) with itself.

This way the function return will yield true only when, and only if, the supplied argument-return, is a failed attempt of conversion to a number object, i.e., a not-a-number number; e.g., NaN.


isNaNstatic( )

However, for a Static Type Operator - if needed and when needed - we can write a far simpler function such as:

function isNaNstatic(x){   
   return x != x;
}

And avoid the DTC altogether so that if the argument is not explicitly a NaN number, it will return false. Wherefore, testing against the following:

isNaNStatic(" x"); // will return false because it's still a string.

However:
isNaNStatic(1/"x"); // will of course return true. as will for instance isNaNStatic(NaN); >> true.

But contrary to isNaN, the isNaNStatic("NaN"); >> false because it (the argument) is an ordinary string.

p.s.:
The static version of isNaN can be very useful in modern coding scenarios. And it may very well be one of the main reasons I took my time for posting this.

Regards.

反目相谮 2024-07-26 15:52:30

isNAN() 是一个判断给定参数是否为非法数字的函数。
isNaN 将参数类型转换为 Number 类型。 如果你想检查参数是否是数字? 请使用 jQuery 中的 $.isNumeric() 函数。

即 isNaN(foo) 等价于 isNaN(Number(foo))
出于显而易见的原因,它接受所有数字都为数字的任何字符串。 对于前。

isNaN(123) //false
isNaN(-1.23) //false
isNaN(5-2) //false
isNaN(0) //false
isNaN('123') //false
isNaN('Hello') //true
isNaN('2005/12/12') //true
isNaN('') //false
isNaN(true) //false
isNaN(undefined) //true
isNaN('NaN') //true
isNaN(NaN) //true
isNaN(0 / 0) //true

isNAN(<argument>) is a function to tell whether given argument is illegal number.
isNaN typecasts the arguments into Number type. If you want to check if argument is Numeric or not? Please use $.isNumeric() function in jQuery.

That is, isNaN(foo) is equivalent to isNaN(Number(foo))
It accepts any strings having all numerals as numbers for obvious reasons. For ex.

isNaN(123) //false
isNaN(-1.23) //false
isNaN(5-2) //false
isNaN(0) //false
isNaN('123') //false
isNaN('Hello') //true
isNaN('2005/12/12') //true
isNaN('') //false
isNaN(true) //false
isNaN(undefined) //true
isNaN('NaN') //true
isNaN(NaN) //true
isNaN(0 / 0) //true
夜清冷一曲。 2024-07-26 15:52:30

当检查带有空格或 " " 的某个字符串值是否为 isNaN 时,可能会尝试执行字符串验证,例如:

// value = "123 "
if (value.match(/\s/) || isNaN(value)) {
// 做一点事
}

When checking if certain string value with whitespace or " "is isNaN maybe try to perform string validation, example :

// value = "123 "
if (value.match(/\s/) || isNaN(value)) {
// do something
}

谁的年少不轻狂 2024-07-26 15:52:30

我发现拥有一个特定于 Number 类的方法(因为其他执行转换(如 parseInt)的函数对于其中一些值有不同的输出)并使用原型继承是很方便的。

Object.assign(Number.prototype, {
  isNumericallyValid(num) {
    if (
      num === null
      || typeof num === 'boolean'
      || num === ''
      || Number.isNaN(Number(num))
    ) {
      return false;
    }
    return true;
  }
});

I find it convenient to have a method specific to the Number class (since other functions that do conversions like parseInt have different outputs for some of these values) and use prototypal inheritance.

Object.assign(Number.prototype, {
  isNumericallyValid(num) {
    if (
      num === null
      || typeof num === 'boolean'
      || num === ''
      || Number.isNaN(Number(num))
    ) {
      return false;
    }
    return true;
  }
});
幸福还没到 2024-07-26 15:52:30

我使用以下内容。

x=(isNaN(parseFloat(x)))? 0.00:parseFloat(x);

I use the following.

x=(isNaN(parseFloat(x)))? 0.00 : parseFloat(x);

人间不值得 2024-07-26 15:52:30
let isNotNumber = val => isNaN(val) || (val.trim && val.trim() === '');

console.log(isNotNumber(' '));
console.log(isNotNumber('1'));
console.log(isNotNumber('123x'));
console.log(isNotNumber('x123'));
console.log(isNotNumber('0'));
console.log(isNotNumber(3));
console.log(isNotNumber('    x'));
console.log(isNotNumber('1.23'));
console.log(isNotNumber('1.23.1.3'));

if(!isNotNumber(3)){
  console.log('This is a number');
}

let isNotNumber = val => isNaN(val) || (val.trim && val.trim() === '');

console.log(isNotNumber(' '));
console.log(isNotNumber('1'));
console.log(isNotNumber('123x'));
console.log(isNotNumber('x123'));
console.log(isNotNumber('0'));
console.log(isNotNumber(3));
console.log(isNotNumber('    x'));
console.log(isNotNumber('1.23'));
console.log(isNotNumber('1.23.1.3'));

if(!isNotNumber(3)){
  console.log('This is a number');
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文