Java 将负数设为正数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/493494/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:20:00  来源:igfitidea点击:

Make a negative number positive

javanegative-number

提问by Tray

I have a Java method in which I'm summing a set of numbers. However, I want any negatives numbers to be treated as positives. So (1)+(2)+(1)+(-1) should equal 5.

我有一个 Java 方法,用于对一组数字求和。但是,我希望任何负数都被视为正数。所以 (1)+(2)+(1)+(-1) 应该等于 5。

I'm sure there is very easy way of doing this - I just don't know how.

我相信有很简单的方法可以做到这一点 - 我只是不知道如何。

采纳答案by Jon Skeet

Just call Math.abs. For example:

只需调用Math.abs。例如:

int x = Math.abs(-5);

Which will set xto 5.

哪个将设置x5.

回答by Uri

Are you asking about absolute values?

你问的是绝对值吗?

Math.abs(...) is the function you probably want.

Math.abs(...) 是您可能想要的函数。

回答by Paul Tomblin

The concept you are describing is called "absolute value", and Java has a function called Math.absto do it for you. Or you could avoid the function call and do it yourself:

您所描述的概念称为“绝对值”,Java 有一个名为Math.abs的函数可以为您完成。或者你可以避免函数调用并自己完成:

number = (number < 0 ? -number : number);

or

或者

if (number < 0)
    number = -number;

回答by jpalecek

Use the absfunction:

使用abs函数:

int sum=0;
for(Integer i : container)
  sum+=Math.abs(i);

回答by Eddie

The easiest, if verbose way to do this is to wrap each number in a Math.abs() call, so you would add:

最简单但冗长的方法是将每个数字包装在 Math.abs() 调用中,因此您可以添加:

Math.abs(1) + Math.abs(2) + Math.abs(1) + Math.abs(-1)

with logic changes to reflect how your code is structured. Verbose, perhaps, but it does what you want.

逻辑更改以反映您的代码的结构。可能是冗长的,但它可以满足您的需求。

回答by Hexagon Theory

You're looking for absolute value, mate. Math.abs(-5)returns 5...

你在寻找绝对价值,伙计。Math.abs(-5)返回 5...

回答by Henrik Paul

You want to wrap each number into Math.abs(). e.g.

您想将每个数字包装成Math.abs(). 例如

System.out.println(Math.abs(-1));

prints out "1".

打印出“1”。

If you want to avoid writing the Math.-part, you can include the Math util statically. Just write

如果您想避免编写Math.-part,您可以静态包含 Math 实用程序。写就好了

import static java.lang.Math.abs;

along with your imports, and you can refer to the abs()-function just by writing

连同您的导入,您可以abs()通过编写来引用-function

System.out.println(abs(-1));

回答by Santhosh

I needed the absolute value of a long , and looked deeply into Math.abs and found that if my argument is less than LONG.MIN_VAL which is -9223372036854775808l, then the abs function would not return an absolute value but only the minimum value. Inthis case if your code is using this abs value further then there might be an issue.

我需要 long 的绝对值,并深入研究 Math.abs 并发现如果我的参数小于 LONG.MIN_VAL 即 -9223372036854775808l,那么 abs 函数将不会返回绝对值,而只会返回最小值。在这种情况下,如果您的代码进一步使用此 abs 值,则可能存在问题。

回答by AZ_

This code is notsafe to be called on positive numbers.

此代码是不是安全上的正数被调用。

int x = -20
int y = x + (2*(-1*x));
// Therefore y = -20 + (40) = 20

回答by Ramkumar

String s = "-1139627840";
BigInteger bg1 = new BigInteger(s);
System.out.println(bg1.abs());

Alternatively:

或者:

int i = -123;
System.out.println(Math.abs(i));