BigDecimal subtract(BigDecimal subtrahend)

优质
小牛编辑
117浏览
2023-12-01

描述 (Description)

java.math.BigDecimal.subtract(BigDecimal subtrahend)返回一个BigDecimal,其值为(this - subtrahend),其标度为max(this.scale(),subtrahend.scale())。

声明 (Declaration)

以下是java.math.BigDecimal.subtract()方法的声明。

public BigDecimal subtract(BigDecimal subtrahend)

参数 (Parameters)

subtrahend - 要从此BigDecimal中减去的值。

返回值 (Return Value)

使用减数后减去BigDecimal对象的值后返回BigDecimal对象的值,即this - subtrahend。

异常 (Exception)

NA

例子 (Example)

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

package cn.xnip;
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