當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java BigInteger signum()用法及代碼示例


先決條件: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()



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 BigInteger signum() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。