我是靠谱客的博主 聪明路人,这篇文章主要介绍Java BigDecimal减去()方法与示例,现在分享给大家,希望可以做个参考。

BigDecimal类的减去()方法 (BigDecimal Class subtract() method)

Syntax:

句法:

复制代码
1
2
3
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:

例:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// 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

输出量

复制代码
1
2
3
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内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(82)

评论列表共有 0 条评论

立即
投稿
返回
顶部