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


Java BigDecimal signum()用法及代码示例


java.math.BigDecimal.signum()是Java中的内置方法,该方法返回此BigDecimal的signum函数。符号函数或符号函数是提取实数符号的奇数数学函数。在数学表达式中,符号函数通常表示为sgn。

用法:

public int signum()

参数:此方法不接受任何参数。


返回值:此方法可以返回三种类型的值:

  • 如果此BigDecimal<0, 则为-1
  • 如果此BigDecimal = 0,则为0
  • 如果此BigDecimal> 0, 则为1

以下示例程序旨在说明上述方法的用法:

示例1:

// Program to demonstrate signum() method of BigDecimal  
  
import java.math.*; 
  
public class Gfg { 
  
    public static void main(String[] args) 
    { 
  
        BigDecimal b1 = new BigDecimal("12743"); 
        BigDecimal b2 = new BigDecimal("0"); 
        BigDecimal b3 = new BigDecimal("-4512"); 
  
        // Assigning the signum values of  BigDecimal objects b1, b2, b3 
        // to  int objects i1, i2, i3 respectively 
        int i1 = b1.signum(); 
        int i2 = b2.signum(); 
        int i3 = b3.signum(); 
  
        // Printing i1, i2, i3 values 
        System.out.println("Signum function on " + b1 + " is " + i1); 
        System.out.println("Signum function on " + b2 + " is " + i2); 
        System.out.println("Signum function on " + b3 + " is " + i3); 
    } 
}
输出:
Signum function on 12743 is 1
Signum function on 0 is 0
Signum function on -4512 is -1

示例2:

// Program to demonstrate signum() method of BigDecimal  
  
import java.math.*; 
  
public class Gfg { 
  
    public static void main(String[] args) 
    { 
  
        BigDecimal b1 = new BigDecimal("17845452743"); 
        BigDecimal b2 = new BigDecimal("000"); 
        BigDecimal b3 = new BigDecimal("-444512"); 
  
        // Assigning the signum values of  BigDecimal objects b1, b2, b3 
        // to  int objects i1, i2, i3 respectively 
        int i1 = b1.signum(); 
        int i2 = b2.signum(); 
        int i3 = b3.signum(); 
  
        // Printing i1, i2, i3 values 
        System.out.println("Signum function on " + b1 + " is " + i1); 
        System.out.println("Signum function on " + b2 + " is " + i2); 
        System.out.println("Signum function on " + b3 + " is " + i3); 
    } 
}
输出:
Signum function on 17845452743 is 1
Signum function on 0 is 0
Signum function on -444512 is -1

参考: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#signum()



相关用法


注:本文由纯净天空筛选整理自Twinkl Bajaj大神的英文原创作品 BigDecimal signum() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。