java 方法 示例

BigDecimal类divideAndRemainder()方法 (BigDecimal Class divideAndRemainder() method)

Syntax:

句法:

    public BigDecimal add(BigDecimal val);
public BigDecimal add(BigDecimal val, MathContext ma_co);

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

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

  • divideAndRemainder(BigDecimal divsr) method is used to get an array of BigDecimal of two elements (the quotient and the remainder), it first divides by using divideToIntegralValue() followed by the result of the remainder on the two values of “BigDecimal” type manipulated.

    splitAndRemainder(BigDecimal divsr)方法用于获取两个元素(商和余数)的BigDecimal数组,它首先使用divideToIntegralValue()进行除法,然后对所处理的“ BigDecimal”类型的两个值进行余数运算。

  • divideAndRemainder(BigDecimal divsr, MathContext ma_co) method is used to get an array of BigDecimal of two elements (the quotient and the remainder), it first divides by using divideToIntegralValue() followed by the result of the remainder on the two values of “BigDecimal” type manipulated along with rounding based on the given MathContext settings.

    splitAndRemainder(BigDecimal divsr,MathContext ma_co)方法用于获取两个元素(商和余数)的BigDecimal数组,它首先使用divideToIntegralValue()进行除法,然后对“ BigDecimal”的两个值进行余数运算”类型,并根据给定的MathContext设置舍入。

  • These methods may throw an exception at the time of calculating the remainder.

    这些方法在计算余数时可能会引发异常。

    ArithmeticException: This exception may throw when the given parameter holds the value 0 or when the result is not accurate and set the rounding mode "UNNECESSARY".

    ArithmeticException :当给定参数的值保持为0或结果不正确并设置舍入模式“ 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, divideAndRemainder(BigDecimal divsr),

    在第一种情况下, DividAndRemainder(BigDecimal divsr)

    • BigDecimal divsr – represents the divisor by this BigDecimal value is to be divided and generate remainder.
    • BigDecimal divsr –表示要被该BigDecimal值除数并生成余数的除数。
  • In the first case, divideAndRemainder(BigDecimal divsr, MathContext ma_co),

    在第一种情况下, dividAndRemainder(BigDecimal divsr,MathContext ma_co)

    • BigDecimal divsr – Similar as defined in the first case.
    • BigDecimal divsr –与第一种情况下定义的相似。
    • 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[], it returns an array of BigDecimal of two element , the first element is the quotient and last element is remainder.

在这两种情况下,方法的返回类型都是BigDecimal [] ,它返回两个元素的BigDecimal数组,第一个元素是商,最后一个元素是余数。

Example:

例:

// Java program to demonstrate the example
// of divideAndRemainder() method of BigDecimal
import java.math.*;
public class DivideAndRemainderOfBD {public static void main(String args[]) {// Initialize three variables divi1,
// divi2 and str
int divi1 = 120;
int divi2 = 4;
String str = "5.6";
// Initialize three BigDecimal objects and
// one MathContext
BigDecimal b_dec1 = new BigDecimal(divi1);
BigDecimal b_dec2 = new BigDecimal(divi2);
BigDecimal b_dec3 = new BigDecimal(str);
MathContext ma_co = new MathContext(3, RoundingMode.CEILING);
System.out.println("divideAndRemainder(BigDecimal): ");
// divides this BigDecimal (b_dec1) by the
// given BigDecimal (b_dec2) and return the BigDecimal[]
// of two values (Quotient, Remainder)
BigDecimal[] div_rem = b_dec1.divideAndRemainder(b_dec2);
System.out.println("Quotient div_rem[0]: " + div_rem[0]);
System.out.println("Remainder div_rem[1]: " + div_rem[1]);
System.out.println(" ");
System.out.println("divideAndRemainder(BigDecimal,MathContext): ");
// divides this BigDecimal (b_dec1) by the
// given BigDecimal (b_dec3) based on the given context setting
// and return the BigDecimal[] of two values (Quotient, Remainder)
div_rem = b_dec1.divideAndRemainder(b_dec3, ma_co);
System.out.println("Quotient div_rem[0]: " + div_rem[0]);
System.out.println("Remainder div_rem[1]: " + div_rem[1]);
}
}

Output

输出量

divideAndRemainder(BigDecimal):
Quotient div_rem[0]: 30
Remainder div_rem[1]: 0divideAndRemainder(BigDecimal,MathContext):
Quotient div_rem[0]: 21
Remainder div_rem[1]: 2.4

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

java 方法 示例

java 方法 示例_Java BigDecimaldividAndRemainder()方法与示例相关推荐

  1. java 方法引用_JAVA 8 方法引用 - Method References

    什么是方法引用 简单地说,就是一个Lambda表达式.在Java 8中,我们会使用Lambda表达式创建匿名方法,但是有时候,我们的Lambda表达式可能仅仅调用一个已存在的方法,而不做任何其它事,对 ...

  2. java组合与继承始示例_Java 8特性与示例

    java组合与继承始示例 Java 8 was released on 18th March 2014, so it's high time to look into Java 8 Features. ...

  3. java组合与继承始示例_Java 9功能与示例

    java组合与继承始示例 Java 9 is a major release and it has brought us a lot of features for developers. In th ...

  4. java内部类写法_Java匿名内部类的写法示例

    前言 在Java中调用某个方法时,如果该方法的参数是一个接口类型,除了可以传入一个参数接口实现类,还可以使用匿名内部类实现接口来作为该方法的参数. 匿名内部类其实就是没有名称的内部类,在调用包含有接口 ...

  5. java finalize逃脱_java finalize方法详解

    1. finalize的作用 finalize()是Object的protected方法,子类可以覆盖该方法以实现资源清理工作,GC在回收对象之前调用该方法. finalize()与C++中的析构函数 ...

  6. java发布合约_Java智能合约开发示例

    # Java智能合约示例 * * * * * ~~~ package test.contract; import java.io.Serializable; import com.juzix.juic ...

  7. java正则表达式教程_Java正则表达式教程及示例

    [感谢 @CuGBabyBeaR 的热心翻译.如果其他朋友也有不错的原创或译文,可以尝试投递到 ImportNew.] 当我开始我的Java职业生涯的时候,对于我来说正则表达式简直是个是梦魇.本教程旨 ...

  8. java 静态方法覆盖_Java中方法的覆盖和静态方法的隐藏

    下面的程序对巴辛吉小鬣狗和其它狗之间的行为差异进行了建模.如果你不知道 什么是巴辛吉小鬣狗,那么我告诉你,这是一种产自非洲的小型卷尾狗,它们从 来都不叫唤.那么,这个程序将打印出什么呢? class ...

  9. java 传入参数_Java 中方法参数的传递

    java中方法参数传递看似有两种,值传递和引用传递.而实际上都是值传递. 所谓值传递,传递的是变量的副本就是说,在将变量a作为参数传进方法中时,方法中接收到的是a的副本,在方法中对参数的修改实际上修改 ...

  10. java native 例子_Java native方法以及JNI实践

    前言 今天看AndFix实现时,核心方法之ReplaceMethod方法是一个native方法,之前并没有遇到过,所以在此整理记录. native的作用 总而言之:native是与C++联合开发的时候 ...

最新文章

  1. MySQL 5.5 服务器变量详解(二)
  2. Gearman的使用
  3. fdisk分区命令详解与fdisk非交互式分区
  4. 开启3D硬件加速导致Virtualbox无法响应
  5. SAP Spartacus list view里router-outlet的填充逻辑
  6. 项目管理软件伙伴https://www.huobanyun.cn/
  7. 读保护_混合ASIL系统中不同安全等级模块间的边界保护
  8. dj鲜生-用户中心-历史购物
  9. XP下安装装SQL2000企业版本
  10. android动态添加控件在指定位置,Android 如何动态添加 View 并显示在指定位置。
  11. xp html5 显卡,最新桌面显卡性能排行 2018年5月显卡天梯图最新完整版
  12. 简化版WIN7安装打印机时提示“打印机无法安装,打印处理器不存在。”正确处理方法
  13. 盛世乐居回应近期股价波动
  14. ps磨皮滤镜插件Portraiture for Mac 破解方法
  15. C#:实现大图片压缩算法(附完整源码)
  16. SpringBoot整合Elasticsearch,中软国际java二面华为面试
  17. java 异或 排序_Java的位运算符详解实例——与()、非(~)、或(|)、异或(^)...
  18. 上帝视角学JAVA- 基础07-类05【2021-08-06】
  19. 满风资产 | 量化研究员招聘(实习+全职)
  20. HDF文件(.h5)的写入与读取

热门文章

  1. New Phytologist 投稿指南
  2. 电子显微镜的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  3. 山西首发大数据产业发展路线图
  4. ZBC成功上线PancakeSwap的糖浆池,并有望在不久上线Binance
  5. 51cto MySQL OCP认证精品班5期视频
  6. ARM 嵌入式Linux开发-2G 3G无线传输(DTU)和路由器(目录介绍)
  7. AndroidUI显示原理及性能优化
  8. 修改自动填充的input背景色
  9. 2022年全球市场人工智能平台总体规模、主要企业、主要地区、产品和应用细分研究报告
  10. Autojs连接电脑