当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。