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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541103.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Analytics API and Customer Data Platform | Segment

Analytics API and Customer Data Platform | Segment

sheets.add示例_Java ArrayDeque add()方法与示例

sheets.add示例ArrayDeque类add()方法 (ArrayDeque Class add() method) add() Method is available in java.lang package. add()方法在java.lang包中可用。 add() Method is used to add the given element at the end of the deque. add()方法用于在双端队列的末尾添加给定元…

arcgis出界址点成果表_界址点成果表打印

#coding: UTF-8importarcpyimportosimporttypesimportstringimportshutilimportsysimportreimportopenpyxl#ws是一个xls的工作表#mode是替换模型,1完全替换,2模糊替换,3 追加替换defreplacexls(ws,mode, text, replaceText):rowsws.max_rowco…

基于Kubernetes的分布式压力测试方案

压力测试是用来检测系统承载能力的有效手段。在系统规模较小的时候,在一台空闲的服务器上使用[ab],[wrk],[siege]等工具发起一定量的并发请求即可得到一个初步的测试结果。但在系统复杂度逐步提高,特别是引入了负载均衡&#xff0…

旋转数组 java_数组中左旋转的Java程序

旋转数组 javaLet’s take an array a[3,4,5,1,0] here we can see after 1 rotation the position of the array element will be a [4,5,1,0,3], after 2 left rotations a[5,1,0,3,4] and so on hence we can see after d rotation the position of the ith element will be…

The output path is not specified for module XXX

新建项目启动时候:The output path is not specified for module XXX 没有为模块XXX指定输出路径。 解决方案 第一步 第二步 第三步

erwin模型导入mysql_使用erwin进行mysql建模

1,定义数据字典,把需要用到的数据类型创建好2,在物理模式下,设置数据字典,修改comment为%AttName这样会默认使用逻辑模式下,实体属性的名称作为字段的注释3,物理模式下选择database->pre & post scr…

EditText焦点问题

1、在一个Activity中加入一个EditText后,每次进入这个Activity时输入法都会自己主动弹出来。非常烦,找了些资料,在此记下解决的方法: 方法:在EditText的父控件中获得焦点。这样焦点就不会自己主动跑到EditText上了。 代…

stl中copy()函数_std :: copy()函数以及C ++ STL中的示例

stl中copy()函数C STL std :: copy()函数 (C STL std::copy() function) copy() function is a library function of algorithm header, it is used to copy the elements of a container, it copies the elements of a container from given range to another container from…

phpmyadmin管理mysql_用phpMyAdmin管理MySQL数据库_MySQL

phpmyadmin学会使用基于Web数据库的管理工具phpMyAdmin。如果使用合适的工具,MySQL数据库的管理就会为得相当简单。应用MySQL命令行方式需要对MySQL知识非常熟悉,对SQL语言也是同样的道理。不仅如此,如果数据库的访问量很大,列表中…

Linux网络那点事

跨平台系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#linux 之前的之前说过网络自连接的配置(CentOS服务器网络配置:http://www.cnblogs.com/dunitian/p/4975830.html),这次和这个类似 这种方法适用于Cent…

机器学习中的马尔可夫随机场模型

马尔可夫随机场 (Markovs Random Fields) Markov random model is a model which use an undirected graph. Undirected graphical models edge represents the potential between two variables, syntactically, Factorization distribution probabilities between variable. …

python爬虫反爬 css 知乎 专栏_反反爬虫系列(四)

过完年,好了,咱们接着更新反反爬虫系列至于之前有朋友表示出一下1688呀,x宝的反反爬虫说实在的,阿里系的反爬虫很厉害,我自愧不能搞定。比如x宝的登录,用了selenium chrome的朋友都会遇到滑条拖动验证失败…

javaweb中mysql数据库的回滚操作代码

2019独角兽企业重金招聘Python工程师标准>>> 在mysql中创建用户账户数据库(注意,count不能为负数,要设置无符号型) 添加数据 下面我们得到connection对象开始进行事务提交和回滚的操作 package com.lyb.test; import s…

ruby array_Ruby中带有示例的Array.shuffle方法

ruby arrayArray.shuffle方法 (Array.shuffle Method) In this article, we will study about Array.shuffle method. You all must be thinking the method must be doing something which is related to shuffling of elements or objects in the Array instance. It is not …

【147天】尚学堂高淇Java300集视频精华笔记(108-109)

第108集:容器equals和hashcodeJDK源代码分析 本集知识点 Java中规定,若两个对象equals比较后内容相等(为true),则hashCode必须相等,反之不然。【原因见内存分析图】hashCode与equals方法必须同时重写,且必须…

ruby hash方法_Ruby中带有示例的Hash.key?(obj)方法

ruby hash方法Hash.key?(obj)方法 (Hash.key?(obj) Method) In this article, we will study about Hash.key?(obj) Method. The working of the method cant be assumed because of its quite a different name. Let us read its definition and understand its …

python迭代器与生成器答案_史上最全 Python 迭代器与生成器

原标题:史上最全 Python 迭代器与生成器作者:浪子燕青链接:http://www.langzi.fun/迭代器与生成器.html迭代器与可迭代对象概念迭代器:是访问数据集合内元素的一种方式,一般用来遍历数据,但是他不能像列表一…

[性能测试] LoadRunner结果分析 – TPS

本文转载自:http://www.tuicool.com/articles/6z6vuy针对吞吐率和 TPS 的关系,这个在结果分析中如何使用,就个人经验和朋友讨论后,提出如下建议指导,欢迎同僚指正。相关定义响应时间 网络响应时间 应用程序响应时间响…

密码学电子书_密码学中的电子密码书(ECB)

密码学电子书This Electronic Code Book (ECB) is cryptography as a mode of operation for a block cipher, with the characters the main things that every feasible block of plaintext or an original text has a corresponding characteristic of ciphertext value and…