BigDecimal类的negate()方法 (BigDecimal Class negate() method)

Syntax:

句法:

    public BigDecimal negate();
public BigDecimal negate(MathContext ma_co);

  • negate() method is available in java.math package.

    negate()方法在java.math包中可用。

  • negate() method is used to get a negation of this BigDecimal and its scale is non-negative.

    negate()方法用于获取此BigDecimal的取反,并且其标度为非负数。

  • negate(MathContext ma_co) method is used to retrieve a negation of this BigDecimal along with rounding based on the given MathContext settings.

    negate(MathContext ma_co)方法用于检索此BigDecimal的取反以及基于给定MathContext设置的舍入。

  • These methods may throw an exception at the time of the negation of 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, negate(),

    在第一种情况下, negate()

    • It does not accept any parameter.
  • In the first case, negate(MathContext ma_co),

    在第一种情况下, negate(MathContext ma_co)

    • 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 negation of this BigDecimal.

    在第一种情况下,它返回此BigDecimal的取反。

  • In the second case, it returns the negation of this BigDecimal. with rounding if required.

    在第二种情况下,它返回此BigDecimal的取反。 如果需要,请四舍五入。

Example:

例:

// Java program to demonstrate the example
// of negate() method of BigDecimal
import java.math.*;
public class NegateOfBD {public static void main(String args[]) {// Initialize two variables val1,
// val2
String val1 = "-10.30";
int val2 = 4;
// Initialize two BigDecimal objects and
// one MathContext
BigDecimal b_dec1 = new BigDecimal(val1);
BigDecimal b_dec2 = new BigDecimal(val2);
MathContext ma_co = new MathContext(2, RoundingMode.CEILING);
System.out.println("negate(): ");
// returns the negate value of this BigDecimal
// like (-b_dec2)
BigDecimal negate = b_dec2.negate();
System.out.println("b_dec2.negate(): " + negate);
System.out.println(" ");
System.out.println("negate(MathContext): ");
// returns the negate value of this BigDecimal
// (-b_dec1) and return the result based on the given
// context settings
negate = b_dec1.negate(ma_co);
System.out.println("b_dec1.negate(ma_co): " + negate);
}
}

Output

输出量

negate():
b_dec2.negate(): -4negate(MathContext):
b_dec1.negate(ma_co): 11

翻译自: https://www.includehelp.com/java/bigdecimal-negate-method-with-example.aspx

Java BigDecimal negate()方法与示例相关推荐

  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. 以Linux系统上的gcc为例,解密C语言编译背后的全过程!
  2. express的cookie解析和签名源码解析
  3. 高德地图 JS API - 根据地名实现标记定位
  4. 【多线程】C++11进行多线程开发 (std::thread)
  5. leetcode170. 两数之和 III - 数据结构设计
  6. Python- 解决PIP下载安装速度慢
  7. Qt文档阅读笔记-Qt4 Lower-Level API扩展Qt Applications(Qt4中Plugin的使用)解析与实例
  8. leetcode 删除链表中的节点
  9. Thinkphp5 模型 验证器执行顺序问题
  10. php 随机数 名称,php – 从标题更改为随机数
  11. centos php7 redis,CentOS7 yum快速安装php7.1+nginx+mysql+redis
  12. Linux系统如何连接和上传文件到服务器上
  13. 关于Redis的常识
  14. Docker Swarm架构、特性与基本实践
  15. 初学C++-----------------类的定义
  16. Java集合类源码详解
  17. 华为网络安全工程师:HCNA-Security(H12-711)题型解析(第二部分)-马青-专题视频课程...
  18. rms 公式 有效值_有效值、真有效值、基波有效值、全有效值概念辨析
  19. 推荐给DBA的Oracle书籍
  20. 紫砂壶的起源 计算机操作题,紫砂壶的起源与历史发展你知道吗?

热门文章

  1. linux课堂笔记(6)
  2. Linux基础(存储结构和磁盘划分)
  3. oracle的undo表空间不足,undo表空间不足,ORA-30036 unable to extend segment by ...
  4. win10 利用安装包 安装 ssh
  5. Nginx(一):概念基础
  6. 《驯狮记——Mac OS X 10.8 Mountain Lion使用手册》——2.3 Dock
  7. 《Spark核心技术与高级应用》——3.2节构建Spark的开发环境
  8. 2月第3周国内域名商TOP10:爱名网排名升至第八
  9. HTTPS 能否避免流量劫持?
  10. MVC Layout布局系统