JavaScript 中的正数转负数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5574144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Positive Number to Negative Number in JavaScript?
提问by Oscar Godson
Basically, the reverse of abs. If I have:
基本上,与abs相反。如果我有:
if ($this.find('.pdxslide-activeSlide').index() < slideNum - 1) {
slideNum = -slideNum
}
console.log(slideNum)
No matter what console always returns a positive number. How do I fix this?
不管什么控制台总是返回一个正数。我该如何解决?
If I do:
如果我做:
if ($this.find('.pdxslide-activeSlide').index() < slideNum - 1) {
_selector.animate({
left: (-slideNum * sizes.images.width) + 'px'
}, 750, 'InOutPDX')
} else {
_selector.animate({
left: (slideNum * sizes.images.width) + 'px'
}, 750, 'InOutPDX')
}
it works tho, but it's not "DRY" and just stupid to have an entire block of code JUST for a -
.
它可以工作,但它不是“干”的,只是为了一个-
.
回答by RichardTheKiwi
Math.abs(num) => Always positive
-Math.abs(num) => Always negative
You do realize however, that for your code
但是,您确实意识到,对于您的代码
if($this.find('.pdxslide-activeSlide').index() < slideNum-1){ slideNum = -slideNum }
console.log(slideNum)
If the index found is 3 and slideNum is 3,
then 3 < 3-1 => false
so slideNum remains positive??
如果找到的索引是 3 并且 slideNum 是 3,
那么 3 < 3-1 => false
所以 slideNum保持正值??
It looks more like a logic error to me.
对我来说,这看起来更像是一个逻辑错误。
回答by Jakub Hampl
The reverse of abs is Math.abs(num) * -1
.
abs 的反面是Math.abs(num) * -1
。
回答by Blake
The basic formula to reverse positive to negative or negative to positive:
将正转负或负转正的基本公式:
i - (i * 2)
回答by Benjamin Williams
To get a negative version of a number in JavaScript you can always use the ~
bitwise operator.
要在 JavaScript 中获得数字的负数,您始终可以使用~
按位运算符。
For example, if you have a = 1000
and you need to convert it to a negative, you could do the following:
例如,如果您有a = 1000
并且需要将其转换为负数,则可以执行以下操作:
a = ~a + 1;
Which would result in a
being -1000
.
这将导致a
是-1000
。
回答by Vivin Paliath
Are you sure that control is going into the body of the if
? As in does the condition in the if
ever hold true? Because if it doesn't, the body of the if
will never get executed and slideNum
will remain positive. I'm going to hazard a guess that this is probably what you're seeing.
你确定控制进入了if
?就像在if
永远中的条件一样吗?因为如果不这样做, 的主体if
将永远不会被执行并且slideNum
将保持正值。我要冒险猜测这可能就是你所看到的。
If I try the following in Firebug, it seems to work:
如果我在 Firebug 中尝试以下操作,它似乎有效:
>>> i = 5; console.log(i); i = -i; console.log(i);
5
-5
slideNum *= -1
should also work. As should Math.abs(slideNum) * -1
.
slideNum *= -1
也应该工作。应该的Math.abs(slideNum) * -1
。
回答by tpayne84
var x = 100;
var negX = ( -x ); // => -100
回答by Ash Pettit
If you don't feel like using Math.Abs * -1 you can you this simple if statement :P
如果您不想使用 Math.Abs * -1,您可以使用这个简单的 if 语句:P
if (x > 0) {
x = -x;
}
Of course you could make this a function like this
当然你可以把它变成这样的功能
function makeNegative(number) {
if (number > 0) {
number = -number;
}
}
makeNegative(-3) => -3 makeNegative(5) => -5
makeNegative(-3) => -3 makeNegative(5) => -5
Hope this helps! Math.abs will likely work for you but if it doesn't this little
希望这可以帮助!Math.abs 可能会为你工作,但如果它不是那么一点点
回答by Jeff Price
var i = 10;
i = i / -1;
Result: -10
结果: -10
var i = -10;
i = i / -1;
Result: 10
结果: 10
If you divide by negative 1, it will always flip your number either way.
如果您除以负 1,则无论哪种方式,它都会翻转您的数字。
回答by Vibhor Dube
num * -1
This would do it for you.
这会为你做的。
回答by milesman34
Use 0 - x
使用 0 - x
x being the number you want to invert
x 是您要反转的数字