概述
BigDecimal类的减去()方法 (BigDecimal Class subtract() method)
Syntax:
句法:
public BigDecimal subtract(BigDecimal val);
public BigDecimal subtract(BigDecimal val, MathContext ma_co);
subtract() method is available in java.math package.
exclude()方法在java.math包中可用。
subtract(BigDecimal val) method is used to get a BigDecimal that holds the value subtracted the given parameter (val) from this BigDecimal and its scale is calculated by using max([this BigDecimal.scale()] , [BigDecimal val.scale()]).
减去(BigDecimal val)方法用于获取一个BigDecimal,该BigDecimal保留从此BigDecimal减去给定参数(val)的值,并使用max([thisBigDecimal.scale()],[BigDecimal val.scale( )])。
subtract(BigDecimal val, MathContext ma_co) method is used to get a BigDecimal that holds the value substracted the given parameter (val) from this BigDecimal based on the given MathContext settings.
减去(BigDecimal val,MathContext ma_co)方法用于获取一个BigDecimal,该BigDecimal包含基于给定MathContext设置从该BigDecimal中减去给定参数(val)的值。
These methods may throw an exception at the time of substracting 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, subtract(BigDecimal val),
在第一种情况下, 减去(BigDecimal val) ,
- BigDecimal val – represents the object is to be substracted from this BigDecimal.
- BigDecimal val –表示要从此BigDecimal中减去对象。
In the first case, subtract(BigDecimal val, MathContext ma_co),
在第一种情况下, 减去(BigDecimal val,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 value substracted the given parameter from this object.
在第一种情况下,它返回从该对象中减去给定参数的值。
In the second case, it returns the value substracted the given parameter from this object with rounding "NECESSARY".
在第二种情况下,它通过舍入“ NECESSARY”返回从该对象中减去给定参数的值。
Example:
例:
// Java program to demonstrate the example
// of subtract() method of BigDecimal
import java.math.*;
public class SubstractOfBD {
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);
// substracts the given BigDecimal b_dec2 from
// this BigDecimal b_dec1 and store it in a
// variable named sub_val
BigDecimal sub_val = b_dec1.subtract(b_dec2);
System.out.println("b_dec1.subtract(b_dec2): " + sub_val);
// substracts the given BigDecimal b_dec2 from
// this BigDecimal b_dec1 and store it in a
// variable named sub_val
sub_val = b_dec1.subtract(b_dec2, ma_co);
System.out.println("b_dec1.subtract(b_dec2,ma_co): " + sub_val);
}
}
Output
输出量
b_dec1.subtract(b_dec2): 117.643
b_dec1.subtract(b_dec2,ma_co): 117.65
翻译自: https://www.includehelp.com/java/bigdecimal-subtract-method-with-example.aspx
最后
以上就是聪明路人为你收集整理的Java BigDecimal减去()方法与示例的全部内容,希望文章能够帮你解决Java BigDecimal减去()方法与示例所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复