Java BigInteger signum()方法
java.math.BigInteger.signum() 方法帮助我们识别一个BigInteger是正数还是零数或负数。它根据以下条件返回以下值之一。
- 当数字为负数时返回-1
- 当数字为零时返回0
- 当数字是正数时,返回+1
语法
public int signum()
参数: 该方法不接受任何参数。
返回值: 当它们是负数、零数或正数时,该方法分别返回-1、0或1作为该BigInteger的值。
举例说明
输入 : 2300
输出 : 1
解释: 2300是正数
所以该方法返回1
输入 : -5482549
输出 : -1
下面的程序说明了BigInteger的signum()方法。
// Program Demonstrate signum() method of BigInteger
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating BigInteger object
BigInteger biginteger = new BigInteger("2300");
// Call signum() method on bigInteger
// store the return value as int
int sigvalue = biginteger.signum();
// Depending upon sign value find sign of biginteger
String sign = null;
if (sigvalue == 0)
sign = "zero";
else if (sigvalue == 1)
sign = "positive";
else
sign = "negative";
// Defining result
String result = "BigInteger " + biginteger + " is " +
sign + " and Sing value is " + sigvalue;
// Print result
System.out.println(result);
}
}
输出
BigInteger 2300 is positive and Sing value is 1
**参考资料: **https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#signum()