
- Java.math 包詳解
- Java.math - 列舉
- Java.math - 討論
Java.math.BigDecimal.subtract() 方法
描述
java.math.BigDecimal.subtract(BigDecimal subtrahend, MathContext mc) 返回的值為 (this - subtrahend),根據上下文設定進行舍入。如果 subtrahend 為零,則使用 this(如果需要,已經舍入)作為結果。如果 this 為零,則結果為 subtrahend.negate(mc)。
宣告
以下是 java.math.BigDecimal.subtract() 方法的宣告。
public BigDecimal subtract(BigDecimal subtrahend, MathContext mc)
引數
subtrahend − 從此 BigDecimal 減去的數值。
mc − 要使用的上下文。
返回值
此方法返回 BigDecimal 物件在減去 subtrahend(即 this - subtrahend)之後的值,根據需要進行舍入。
異常
算術異常 − 如果結果不精確,但舍入模式為 UNNECESSARY
示例
以下示例展示了 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; MathContext mc = new MathContext(2); // 2 precision bg1 = new BigDecimal("100.123"); bg2 = new BigDecimal("50.56"); // subtract bg1 with bg2 using mc and assign result to bg3 bg3 = bg1.subtract(bg2, mc); String str = "The Result of Subtraction is " + bg3; // print bg3 value System.out.println( str ); } }
讓我們編譯並執行以上程式,將產生以下結果:-
The Result of Subtraction is 50
java_math_bigdecimal.htm
廣告