int signum()

优质
小牛编辑
134浏览
2023-12-01

描述 (Description)

java.math.BigDecimal.signum()返回此BigDecimal的signum函数。

声明 (Declaration)

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

public int signum()

参数 (Parameters)

NA

返回值 (Return Value)

此方法返回-1,0或1,因为此BigDecimal的值为负,零或正数。

异常 (Exception)

NA

例子 (Example)

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

package cn.xnip;
import java.math.*;
public class BigDecimalDemo {
   public static void main(String[] args) {
      // create 3 BigDecimal objects
      BigDecimal bg1, bg2, bg3;
      // create 3 int objects
      int i1, i2, i3;
      bg1 = new BigDecimal("123");
      bg2 = new BigDecimal("0");
      bg3 = new BigDecimal("-12");
      // assign the signum values of bg1,bg2,bg3 to i1,i2,i3 respectively
      i1 = bg1.signum();
      i2 = bg2.signum();
      i3 = bg3.signum();
      String str1 = "The Result of Signum function on " + bg1 + " is " + i1;
      String str2 = "The Result of Signum function on " + bg2 + " is " + i2;
      String str3 = "The Result of Signum function on " + bg3 + " is " + i3;
      // print i1,i2,i3 values
      System.out.println( str1 );
      System.out.println( str2 );
      System.out.println( str3 );
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

The Result of Signum function on 123 is 1
The Result of Signum function on 0 is 0
The Result of Signum function on -12 is -1