當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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