Java BigDecimal减去()方法与示例

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值