先決條件:BigInteger基礎
java.math.BigInteger.signum()方法可幫助我們識別BigInteger是正數還是零數或負數。根據以下條件,它將返回以下值之一:
- 當數字為負數時返回-1
- 當數字為零時返回0
- 當數字為正時返回+1
用法:
public int signum()
參數:該方法不帶任何參數。
返回值:當它們分別為負數,零或正數時,此方法返回-1、0或1作為此BigInteger的值。
例子:
Input: 2300 Output: 1 Explanation: 2300 is postive number so the method returns 1 Input: -5482549 Output: -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()
相關用法
- Java Integer signum()用法及代碼示例
- Java BigDecimal signum()用法及代碼示例
- Java StrictMath signum()用法及代碼示例
- Java Long signum()用法及代碼示例
- Java signum()用法及代碼示例
- Java BigInteger or()用法及代碼示例
- Java BigInteger abs()用法及代碼示例
- Java BigInteger xor()用法及代碼示例
- Java BigInteger not()用法及代碼示例
- Java BigInteger and()用法及代碼示例
- Java BigInteger mod()用法及代碼示例
- Java BigInteger pow()用法及代碼示例
- Java BigInteger setBit()用法及代碼示例
- Java BigInteger gcd()用法及代碼示例
- Java BigInteger toByteArray()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 BigInteger signum() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。