Java中的 BigDecimal signum() 方法
Java.math.BigDecimal.signum()是Java中的一个内置方法,它返回这个 BigDecimal 的符号函数。符号函数或符号函数是提取实数符号的奇数数学函数。在数学表达式中,符号函数通常表示为 sgn。
句法:
public int signum()
参数:此方法不接受任何参数。
返回值:该方法可以返回三种类型的值:
- -1 如果这个 BigDecimal < 0
- 如果此 BigDecimal = 0,则为 0
- 1 如果此 BigDecimal > 0
下面的程序说明了上述方法的工作:
方案一:
// 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
方案二:
// 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: Java/math/BigDecimal.html#signum()