BigDecimal类的add()方法 (BigDecimal Class add() method)
Syntax:
句法:
    public BigDecimal add(BigDecimal val);
public BigDecimal add(BigDecimal val, MathContext ma_co);
- add() method is available in java.math package. - add()方法在java.math包中可用。 
- add(BigDecimal val) method is used to get a BigDecimal that holds the value added this BigDecimal with the given BigDecimal and its scale is calculated by using max([this BigDecimal.scale()] , [BigDecimal val.scale()]). - add(BigDecimal val)方法用于获取一个BigDecimal,该BigDecimal保留使用给定BigDecimal与该BigDecimal相加的值,并使用max([thisBigDecimal.scale()],[BigDecimal val.scale()])计算其小数位数。 
- add(BigDecimal val, MathContext ma_co) method is used to get a BigDecimal that holds the value-added this BigDecimal with the given BigDecimal based on the given MathContext settings. - add(BigDecimal val,MathContext ma_co)方法用于获取BigDecimal,该BigDecimal包含基于给定MathContext设置的给定BigDecimal与该BigDecimal的增值。 
- These methods may throw an exception at the time of adding an object. - 这些方法在添加对象时可能会引发异常。 - ArithmeticException: This exception may throw when the result is not accurate and set the rounding mode "UNNECESSARY". - ArithmeticException :当结果不正确并且将舍入模式设置为“ UNNECESSARY”时,可能会引发此异常。 
- These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error. - 这些是非静态方法,可通过类对象访问,如果尝试使用类名访问这些方法,则会收到错误消息。 
Parameter(s):
参数:
- In the first case, add(BigDecimal val), - 在第一种情况下, add(BigDecimal val) , - BigDecimal val – represents the object is to add with this BigDecimal object.
- BigDecimal val –表示对象要与此BigDecimal对象添加。
 
- In the first case, abs(MathContext ma_co), - 在第一种情况下, abs(MathContext ma_co) , - BigDecimal val – Similar as defined in the first case.
- BigDecimal val –与第一种情况下定义的类似。
- MathContext ma_co – represents the context setting to use in rounding.
- MathContext ma_co –表示要舍入的上下文设置。
 
Return value:
返回值:
In both the cases, the return type of the method is BigDecimal,
在这两种情况下,方法的返回类型均为BigDecimal 。
- In the first case, it returns the added result of both the objects without any context setting. - 在第一种情况下,它返回两个对象的相加结果而没有任何上下文设置。 
- In the second case, it returns the added result of both the objects with any context setting. - 在第二种情况下,它将返回具有任何上下文设置的两个对象的相加结果。 
Example:
例:
// Java program to demonstrate the example 
// of add() method of BigDecimal
import java.math.*;
public class AddOfBD {
public static void main(String args[]) {
// Initialize two variables - val,
// and str 
int val = 120;
String str = "2.357";
// Initialize two BigDecimal objects and
// one MathContext
BigDecimal b_dec1 = new BigDecimal(val);
BigDecimal b_dec2 = new BigDecimal(str);
MathContext ma_co = new MathContext(5, RoundingMode.CEILING);
// add this BigDecimal b_dec1 with the given
// BigDecimal b_dec2
BigDecimal add_val = b_dec1.add(b_dec2);
System.out.println("b_dec1.add(b_dec2): " + add_val);
// add this BigDecimal b_dec1 with the given
// BigDecimal b_dec2 based on the given context
// settings
add_val = b_dec1.add(b_dec2, ma_co);
System.out.println("b_dec1.add(b_dec2, ma_co): " + add_val);
}
}
Output
输出量
b_dec1.add(b_dec2): 122.357
b_dec1.add(b_dec2, ma_co): 122.36
翻译自: https://www.includehelp.com/java/bigdecimal-add-method-with-example.aspx