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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。