Javascript负数 [英] Javascript negative number
问题描述
我想检查数字是否为负数。我正在寻找最简单的方法,所以预定义的javascript函数将是最好的,但我还没有发现任何东西,这是我到目前为止但我不认为这是一个好方法:
I want to check if a number is negative. I'm searching for the easiest way, so a predefined javascript function would be the best but I didn't found yet anything, here is what I have so far but I don't think that this is a good way:
function negative(number) {
if (number.match(/^-\d+$/)) {
return true;
} else {
return false;
}
}
推荐答案
写一个函数来做这个检查,你应该只能使用这个表达式:
Instead of writing a function to do this check, you should just be able to use this expression:
(number < 0)
Javascript将首先尝试将左侧转换为数字值,然后再检查是否为小于零,这似乎是你想要的。
Javascript will evaluate this expression by first trying to convert the left hand side to a number value before checking if it's less than zero, which seems to be what you wanted.
x<的行为y
在§11.8.1小于运算符(<
)中指定,它使用 §11.8.5抽象关系比较算法。
如果 x
和 y
是字符串,但由于右侧已经是(数字<0)
中的数字,比较将尝试将左侧转换为数字以进行数字比较。如果左侧无法转换为数字,则结果为 false
。
The situation is a lot different if both x
and y
are strings, but since the right hand side is already a number in (number < 0)
, the comparison will attempt to convert the left hand side to a number to be compared numerically. If the left hand side can not be converted to a number, the result is false
.
请注意这一点与基于正则表达式的方法相比,可能会得到不同的结果,但取决于您尝试做什么,它最终可能会做正确的事情。
Do note that this may give different results when compared to your regex-based approach, but depending on what is it that you're trying to do, it may end up doing the right thing anyway.
-
- 0< 0
是false
,这与-0< 0
也是false
(参见:签名为零)。 -
- Infinity< 0
是true
(无限被确认) -
- 1e0< ; 0
是true
(接受科学记数法文字) -
- 0x1 < 0
是true
(接受十六进制文字) -
- 1 < 0
是true
(允许某些形式的空格)
"-0" < 0
isfalse
, which is consistent with the fact that-0 < 0
is alsofalse
(see: signed zero)."-Infinity" < 0
istrue
(infinity is acknowledged)"-1e0" < 0
istrue
(scientific notation literals are accepted)"-0x1" < 0
istrue
(hexadecimal literals are accepted)" -1 " < 0
istrue
(some forms of whitespaces are allowed)
对于上面的每一个例子,正则表达式方法都会评估相反的结果( true
而不是 false
反之亦然)。
For each of the above example, the regex method would evaluate to the contrary (true
instead of false
and vice versa).
- ECMAScript 5 (PDF)
- ECMAScript 3, §11.8.1 The Less-than Operator (
<
) - ECMAScript 3, §11.8.5 The Abstract Relational Comparison Algorithm
- regular-expressions.info/Matching Floating Point Numbers with a Regular Expression
它还应该可以说这种形式的陈述:
It should also be said that statements of this form:
if (someCondition) {
return valueForTrue;
} else {
return valueForFalse;
}
可以重构为使用三元/条件? :
运营商(§11.12)简单地说:
can be refactored to use the ternary/conditional ?:
operator (§11.12) to simply:
return (someCondition) ? valueForTrue : valueForFalse;
的惯用法?:
可以使代码更简洁,更易读。
Idiomatic usage of ?:
can make the code more concise and readable.
- javascript if alternative
- To Ternary or Not To Ternary?
Javascript具有可以调用以执行各种类型转换的函数。
Javascript has functions that you can call to perform various type conversions.
如下所示:
if (someVariable) {
return true;
} else {
return false;
}
可以使用重构?:
运营商:
return (someVariable ? true : false);
但您还可以进一步将其简化为:
But you can also further simplify this to:
return Boolean(someVariable);
这将布尔
作为一个函数调用(< a href =http://bclary.com/2004/11/07/#a-15.6.1 =noreferrer>§15.16.1)执行所需的类型转换。您可以类似地将 Number
作为函数调用(§15.17.1)执行转换为数字。
This calls Boolean
as a function (§15.16.1) to perform the desired type conversion. You can similarly call Number
as a function (§15.17.1) to perform a conversion to number.
- How can I convert a string to boolean in JavaScript?
- what is the purpose of javascript new Boolean() ?
这篇关于Javascript负数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!