Java中的 BigInteger signum() 方法
先决条件: BigInteger 基础
Java.math.BigInteger.signum()方法帮助我们识别 BigInteger 是正数、零还是负数。它根据以下条件返回以下值之一:
- 当 number 为负数时返回 -1
- 当数字为零时返回 0
- 当数字为正时返回 +1
句法:
public int signum()
参数:该方法不带任何参数。
返回值:该方法分别返回负数、零或正数时的 -1、0 或 1 作为此 BigInteger 的值。
例子:
Input: 2300
Output: 1
Explanation: 2300 is positive number
so the method returns 1
Input: -5482549
Output: -1
下面的程序说明了 BigInteger 的 signum() 方法。
Java
// 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: Java/math/BigInteger.html#signum()