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()
相关用法
- Java BigInteger signum()用法及代码示例
- Java Integer signum()用法及代码示例
- Java StrictMath signum()用法及代码示例
- Java Long signum()用法及代码示例
- Java signum()用法及代码示例
- Java BigDecimal ulp()用法及代码示例
- Java BigDecimal plus()用法及代码示例
- Java BigDecimal min()用法及代码示例
- Java BigDecimal abs()用法及代码示例
- Java BigDecimal max()用法及代码示例
- Java BigDecimal pow()用法及代码示例
- Java BigDecimal valueOf()用法及代码示例
- Java BigDecimal byteValueExact()用法及代码示例
- Java BigDecimal doubleValue()用法及代码示例
- Java BigDecimal toBigIntegerExact()用法及代码示例
注:本文由纯净天空筛选整理自Twinkl Bajaj大神的英文原创作品 BigDecimal signum() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。