Java BigDecimal add()方法与示例
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
Java BigDecimal add()方法与示例相关推荐
- java arraylist.add(),Java ArrayList add()方法与示例
ArrayList类add()方法 语法:public boolean add(T ele); public void add(int indices, T ele);add()方法在java.uti ...
- cdate在java中_Java Calendar.add方法代码示例
本文整理汇总了Java中java.util.Calendar.add方法的典型用法代码示例.如果您正苦于以下问题:Java Calendar.add方法的具体用法?Java Calendar.add怎 ...
- Java IOUtils.copy方法代码示例(亲测)
本文整理汇总了Java中org.apache.commons.io.IOUtils.copy方法的典型用法代码示例.如果您正苦于以下问题:Java IOUtils.copy方法的具体用法?Java I ...
- Java List.add()方法
Java List.add()方法 Java 集合类中的 List.add() 方法用于向集合列表中添加对象. 语法1 add(A) 用于在列表的尾部插入指定元素.如果 List 集合对象由于调用 a ...
- java calendar.add方法_Java Calendar add()方法与示例
日历类add()方法add()方法在java.util包中可用. add()方法用于对指定的cal_fi(日历字段)执行相加或相减的时间量. add()方法是一个非静态方法,可通过类对象访问,如果尝试 ...
- java的add方法的使用_Java HashSet add()方法与示例
HashSet类add()方法add()方法在java.util包中可用. 当尚不存在给定元素时,使用add()方法将其插入此HashSet中,否则它将忽略它并返回false. add()方法是一种非 ...
- BigDecimal add方法问题:调用add后,求和结果没变
BigDecimal的一个比较坑的问题,使用add时,原数值竟然不变. BigDecimal 的 add方法是,调用者不变. 因此,对结果进行接收.改为如下 import java.math.BigD ...
- java iterator set,Java HashSet iterator()方法与示例
HashSet类iterator()方法iterator()方法在java.util包中可用. iterator()方法用于通过使用iterator()方法来迭代此HashSet中存在的所有元素. i ...
- java vector.isempty,Java Vector isEmpty()方法与示例
向量类isEmpty()方法isEmpty()方法在java.util包中可用. isEmpty()方法用于检查此Vector是"空"还是"非空". isEmp ...
最新文章
- 主题分享 | 王建民:关于工业软件人才培养的思考
- 社区奖品之Linux内核设计与实现
- “假一赔十”的4k 120Hz电视能买吗?研究完我服了,水是真的深
- python怎么把程序封装成函数_PYTHON中如何把固定格式代码,封装成一个函数?
- kubernetes_Kubernetes领域的标准安全性。
- Elasticsearch安装X-Pack插件
- 在django中使用vue.js需要注意的地方
- ASP.NET站点性能提升-缩短首页生成时间
- 《麦肯锡方法》读书笔记17
- hilbert变换简介
- 计算机组成原理学习笔记一
- Python绘制饼状图对商品库存进行分析
- Android系统Surface机制的SurfaceFlinger服务简要介绍和学习计划
- 猿创征文 | 专做药品生产研发的程序员
- Android软键盘弹出和收起的监听
- 小型气象站参数有哪些
- XX健康:移动端开发-体检预约验证码30秒倒计时短信验证码获取与验证DatePicker日历展示提交预约复杂流程阿里短信工具类
- 对一支圆珠笔进行测试,要从哪些方面进行测试?
- Elasticsearch:索引状态是红色还是黄色?为什么?
- 精通linux内核年薪,最新IT人才薪酬体系一览表
热门文章
- poj 3486 A Simple Problem with Integers(树状数组第三种模板改段求段)
- poj1703Find them, Catch them(并查集以及路径压缩)
- 一个小例子对多态简单的理解
- 2021计算机基础知识题库,2021~2021计算机基础知识练习题
- Vim进阶2 map映射
- 当前超级计算机的应用方兴未艾,四川省若尔盖县高三下学期语文模拟卷(五)
- mysql 按月和年累加_广西柳州市市场监管局公布市2020年11月(第一批)电梯按需维保试点名单...
- python显示数据长度_python – 获取CSV的长度以显示进度
- php进程通讯方式,PHP进程模型、进程通讯方式、进程线程的区别分别有哪些?
- inux CentOS 7 修改内核启动默认顺序