在 JavaScript 中使数字值相反
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11508845/
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
Make number Value opposite in JavaScript
提问by Aaron Lumsden
I want to know if it is possible to make a number the opposite to what it currently is using JavaScript. ie if a number is 400
. Is it possible to make it -400
, similar if a number is -400
is it possible to make it 400
?
我想知道是否有可能使一个数字与当前使用 JavaScript 的数字相反。即如果一个数字是400
. 是否有可能做到-400
,类似如果一个数字是否-400
有可能做到400
?
回答by Jay
This is not jQuery!
这不是 jQuery!
Just multiply it by -1
.
只需乘以-1
。
num = "400"
console.log(-num);
回答by InaFK
Core JS,
核心JS,
function opposite(number) {
return(-number);
}
As the shorter solution from http://www.codewars.com/
作为来自http://www.codewars.com/的更短的解决方案