Java.math.BigInteger.signum() 方法



描述

java.math.BigInteger.signum() 返回此 BigInteger 的符号函数。

声明

以下是 java.math.BigInteger.signum() 方法的声明。

public int signum()

参数

不适用

返回值

此方法返回 -1、0 或 1,表示此 BigInteger 的值是负数、零还是正数。

异常

不适用

示例

以下示例显示了 math.BigInteger.signum() 方法的用法。

package com.tutorialspoint;

import java.math.*;

public class BigIntegerDemo {

   public static void main(String[] args) {

      // create 3 BigInteger objects
      BigInteger bi1, bi2, bi3;

      // create 3 int objects
      int i1, i2, i3;

      bi1 = new BigInteger("0");
      bi2 = new BigInteger("10");
      bi3 = new BigInteger("-10");

      // assign signum results of bi1, bi2, bi3 to i1, i2, i3
      i1 = bi1.signum();
      i2 = bi2.signum();
      i3 = bi3.signum();

      String str1 = "Signum function returns " + i1 + " for " +bi1;
      String str2 = "Signum function returns " + i2 + " for " +bi2;
      String str3 = "Signum function returns " + i3 + " for " +bi3;

      // print i1, i2, i3 values
      System.out.println( str1 );
      System.out.println( str2 );
      System.out.println( str3 );
   }
}

让我们编译并运行以上程序,结果如下 −

Signum function returns 0 for 0
Signum function returns 1 for 10
Signum function returns -1 for -10
java_math_biginteger.htm
广告
© . All rights reserved.