Java-math-bigdecimal-add-augend-mc
提供:Dev Guides
Java.math.BigDecimal.add()メソッド
説明
- java.math.BigDecimal.add(BigDecimal augend、MathContext mc)*は、値が(this + augend)であるBigDecimalを返し、MathContext設定に従って丸めます。 いずれかの数値がゼロで、精度設定がゼロ以外の場合、必要に応じて丸められたもう一方の数値が結果として使用されます。
宣言
以下は、* java.math.BigDecimal.add()*メソッドの宣言です。
public BigDecimal add(BigDecimal augend, MathContext mc)
パラメーター
- augend -このBigDecimalに追加する値。
- mc -使用するコンテキスト。
戻り値
このメソッドは、値がthis + augendであり、必要に応じて丸められたBigDecimalオブジェクトを返します
例外
*ArithmeticException* -結果は正確ではないが、丸めモードがUNNECESSARYの場合。
例
次の例は、math.BigDecimal.add()メソッドの使用法を示しています。
package com.finddevguides;
import java.math.*;
public class BigDecimalDemo {
public static void main(String[] args) {
//create 3 BigDecimal objects
BigDecimal bg1,bg2,bg3,bg4;
//assign value to bg1 and bg2
bg1 = new BigDecimal("40.732");
bg2 = new BigDecimal("30.12");
//print bg1 and bg2 value
System.out.println("Object Value is " + bg1);
System.out.println("Augend value is " + bg2);
//create MathContext object with 4 precision
MathContext mc = new MathContext(4);
//perform add operation on bg1 with augend bg2 and context mc
bg3 = bg1.add(bg2,mc);
//print bg3 value with
System.out.println("Result is " + bg3);
}
}
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
Object Value is 40.732
Augend value is 30.12
Result is 70.85