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


Java BigDecimal subtract()用法及代碼示例


java.math.BigDecimal.subtract(BigDecimal val)用於計算兩個BigDecimal的算術差。該方法用於查找大數的算術差異,而不會影響結果的精度。此方法對當前的BigDecimal執行操作,通過該操作調用此方法並將BigDecimal作為參數傳遞。

Java中有兩種減法方法重載,如下所示:

  • subtract(BigDecimal val)
  • subtract(BigDecimal val, MathContext mc)

subtract(BigDecimal val)

用法:


public BigDecimal subtract(BigDecimal val)

參數:此方法接受參數val,該參數是要從此BigDecimal中減去的值。

返回值:此方法返回一個BigDecimal,該BigDecimal保留差異(this – val),其小數位數為max(this.scale(),val.scale())。

下麵的程序用於說明BigDecimal的subtract()方法。

// Java program to demonstrate 
// subtract() method of BigDecimal 
  
import java.math.BigDecimal; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        // BigDecimal object to store result 
        BigDecimal diff; 
  
        // For user input 
        // Use Scanner or BufferedReader 
  
        // Two objects of String created 
        // Holds the values to calculate the difference 
        String input1 
            = "545456468445645468464645"; 
        String input2 
            = "425645648446468486486452"; 
  
        // Convert the string input to BigDecimal 
        BigDecimal a 
            = new BigDecimal(input1); 
        BigDecimal b 
            = new BigDecimal(input2); 
  
        // Using subtract() method 
        diff = a.subtract(b); 
  
        // Display the result in BigDecimal 
        System.out.println("The difference of\n"
                           + a + " \nand\n" + b + " "
                           + "\nis\n" + diff + "\n"); 
    } 
}

輸出:

The difference of
545456468445645468464645
and
425645648446468486486452
is
119810819999176981978193

subtract(BigDecimal val, MathContext mc)

用法:

public BigDecimal subtract(BigDecimal val, MathContext mc)

參數:此方法接受兩個參數,一個是val,它是要從此BigDecimal中減去的值;一個是MathContext類型的mc。

返回值:此方法返回一個BigDecimal,該值保存差異(this – val),並根據上下文設置進行舍入。

下麵的程序用於說明BigDecimal的subtract()方法。

// Java program to demonstrate 
// subtract() method of BigDecimal 
  
import java.math.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        // BigDecimal object to store result 
        BigDecimal diff; 
  
        // For user input 
        // Use Scanner or BufferedReader 
  
        // Two objects of String created 
        // Holds the values to calculate the difference 
        String input1 
            = "468445645468464645"; 
        String input2 
            = "4256456484464684864864"; 
  
        // Convert the string input to BigDecimal 
        BigDecimal a 
            = new BigDecimal(input1); 
        BigDecimal b 
            = new BigDecimal(input2); 
  
        // Set precision to 10 
        MathContext mc 
            = new MathContext(10); 
        // Using subtract() method 
        diff = a.subtract(b, mc); 
  
        // Display the result in BigDecimal 
        System.out.println("The difference of\n"
                           + a + " \nand\n" + b + " "
                           + "\nis\n" + diff + "\n"); 
    } 
}

輸出:

The difference of
468445645468464645
and
4256456484464684864864
is
-4.255988039E+21

參考文獻:https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#subtract(java.math.BigDecimal)



相關用法


注:本文由純淨天空篩選整理自Rajnis09大神的英文原創作品 BigDecimal subtract() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。