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减去()方法与示例相关推荐

  1. Java IOUtils.copy方法代码示例(亲测)

    本文整理汇总了Java中org.apache.commons.io.IOUtils.copy方法的典型用法代码示例.如果您正苦于以下问题:Java IOUtils.copy方法的具体用法?Java I ...

  2. java user directory,Java ProcessBuilder directory()方法与示例

    语法:public File directory (); public ProcessBuilder directory (File dir); ProcessBuilder类directory()方 ...

  3. Java序列化魔术方法及其示例使用

    在上一篇文章中, 您需要了解有关Java序列化的所有知识 ,我们讨论了如何通过实现Java序列化来启用类的可序列化性. Serializable接口. 如果我们的类未实现Serializable接口, ...

  4. catalog java,Java Connection getCatalog()方法与示例

    通常,目录是一个目录,其中包含有关数据集,文件或数据库的信息.而数据库目录中包含所有数据库,基本表,视图(虚拟表),同义词,值范围,索引,用户和用户组的列表. Connection接口的getCata ...

  5. filepermission java,Java FilePermission getActions()方法与示例

    FilePermission类getActions()方法getActions()方法在java.io包中可用. getActions()方法用于检查此FilePermission和给定对象在路径名和 ...

  6. java方法参数Bundle,Java ResourceBundle keySet()方法及示例

    ResourceBundle类keySet()方法keySet()方法在java.util包中可用. keySet()方法用于从此ResourceBundle及其超级捆绑包中获取所有现有键,以在Set ...

  7. java exec waitfor,Java Process waitFor()方法与示例

    流程类waitFor()方法在java.lang包中提供了waitFor()方法. waitFor()方法用于使当前正在运行的线程在需要时等待,直到由该Process对象表示的进程完成其终止为止. 当 ...

  8. java arraylist.add(),Java ArrayList add()方法与示例

    ArrayList类add()方法 语法:public boolean add(T ele); public void add(int indices, T ele);add()方法在java.uti ...

  9. java rollback用法,Java Connection rollBack()方法与示例

    回滚操作将撤消当前事务所做的所有更改,即,如果调用Connection接口的rollBack()方法,则所有修改都将还原到最后一次提交. 您还可以通过将所需的Savepoint对象作为参数传递给此方法 ...

最新文章

  1. html 显示消息数量,html实现消息按钮上的数量角标的实例详解
  2. linux命令之高级使用 find
  3. vue click事件冒泡,默认行为
  4. 深度学习核心技术精讲100篇(十)-机器学习模型融合之Kaggle如何通过Stacking提升模型性能
  5. Office安装源损坏
  6. Laravel中的env函数获取不到确定存在的配置
  7. java一览删除一条数据_可以删除单条数据,不能再返回列表页面,我使用的是Spring MVC...
  8. logstash收集TCP端口日志
  9. 随心订制linux透明防火墙
  10. Windows8.1-KB2999226-x64安装提示此更新不适用你的计算机
  11. i9x系列是服务器CPU吗,Intel推出全新酷睿X系列CPU:i9处理器亮相
  12. 基于NW实现的前端桌面应用
  13. SQLmap注入学习实战 —— dvwa 从low到impossble
  14. 手把手教你在Unity中实现小地图
  15. 【内网渗透】域横向PTHPTKPTT哈希票据传递
  16. 7-13 愿天下有情人都是失散多年的兄妹 (25 分)
  17. Linux配置PHP环境
  18. 【转】linux shell 正则表达式(BREs,EREs,PREs)差异比较
  19. 【opencv450-samples】delaunay 三角剖分和 voronoi 细分
  20. 目前国内p2p视频直播软件

热门文章

  1. 我也是LeetCode周赛“三道题选手”啦 第270场周赛
  2. java 调用微信api发送消息
  3. 李开复给大学生的一些忠告
  4. 作业帮面试题 最长不重复子串
  5. 我是一个杀毒软件线程
  6. 访问前端时,报403 Forbidden问题
  7. JS判断服务端是win还是Linux,js 判断操作系统类型, win7,win10,麒麟都测试过
  8. TikTok网红营销:出海品牌如何借势2022圣诞节?
  9. Shader做剪影效果
  10. pycharm爬虫打印网页出现中文乱码问题