Java.math.BigDecimal.subtract() 方法



描述

java.math.BigDecimal.subtract(BigDecimal subtrahend) 返回一個 BigDecimal,其值為 (this - subtrahend),且其範圍為 max(this.scale(), subtrahend.scale())。

宣告

以下是 java.math.BigDecimal.subtract() 方法的宣告。

public BigDecimal subtract(BigDecimal subtrahend)

引數

subtrahend − 要從此 BigDecimal 中減去的值。

返回值

此方法返回 BigDecimal 物件的值,經過用 subtrahend 進行減法運算後,即 this - subtrahend。

異常

示例

以下示例演示了 math.BigDecimal.subtract() 方法的用法。

package com.tutorialspoint;

import java.math.*;

public class BigDecimalDemo {

   public static void main(String[] args) {

      // create 3 BigDecimal objects
      BigDecimal bg1, bg2, bg3;

      bg1 = new BigDecimal("100.123");
      bg2 = new BigDecimal("50.56");

      // subtract bg1 with bg2 and assign result to bg3
      bg3 = bg1.subtract(bg2);

      String str = "The Result of Subtraction is " + bg3;

      // print bg3 value
      System.out.println( str );
   }
}

讓我們編譯並執行上述程式,它將產生以下結果 -

The Result of Subtraction is 49.563
java_math_bigdecimal.htm
廣告