博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java BigDecimal restder()方法与示例
阅读量:2529 次
发布时间:2019-05-11

本文共 3782 字,大约阅读时间需要 12 分钟。

BigDecimal类的restder()方法 (BigDecimal Class remainder() method)

Syntax:

句法:

public BigDecimal remainder(BigDecimal divsr);    public BigDecimal remainder(BigDecimal divsr, MathContext ma_co);
  • remainder() method is available in java.math package.

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

  • remainder (BigDecimal divsr) method is used to calculate the remainder by using ([this BigDecimal] % divsr).

    余数(BigDecimal divsr)方法用于通过使用([this BigDecimal]%divsr)计算余数。

  • remainder(BigDecimal divsr, MathContext ma_co) method is used to calculate the remainder by using ([this BigDecimal] % divsr) along with rounding based on the given MathContext settings.

    restder(BigDecimal divsr,MathContext ma_co)方法用于通过使用([this BigDecimal]%divsr)以及基于给定MathContext设置的舍入来计算余数。

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

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

    ArithmeticException: This exception may throw when the calculated result is not accurate but the mode of rounding "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, remainder(BigDecimal divsr),

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

    • BigDecimal divsr – represents the divisor by which this BigDecimal value is to be divided for calculating remainder.
    • BigDecimal divsr –表示该BigDecimal值将被除以计算余数的除数。
  • In the first case, remainder(BigDecimal divsr, MathContext ma_co),

    在第一种情况下, 剩下的是(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,

在这两种情况下,方法的返回类型均为BigDecimal 。

  • In the first case, it returns the remainder when this BigDecimal object divides by the given divisor.

    在第一种情况下,当此BigDecimal对象除以给定的除数时,它将返回余数。

  • In the second case, it returns the remainder when this BigDecimal object divides by the given divisor with rounding.

    在第二种情况下,当此BigDecimal对象通过舍入除以给定的除数时,它将返回余数。

Example:

例:

// Java program to demonstrate the example // of remainder() method of BigDecimalimport java.math.*;public class RemainderOfBD {
public static void main(String args[]) {
// Initialize three variables divi1, // divi2 and str String divi1 = "120.12"; String divi2 = "40.55"; String str = "5"; // 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("remainder(BigDecimal): "); // divides this BigDecimal (b_dec1) by the // given BigDecimal (b_dec3) and return the BigDecimal // object that holds the value of the remainder BigDecimal remainder = b_dec1.remainder(b_dec3); System.out.println("b_dec1.remainder(b_dec3): " + remainder); System.out.println(" "); System.out.println("remainder(BigDecimal,MathContext): "); // divides this BigDecimal (b_dec2) by the // given BigDecimal (b_dec3) based on the given // context setting and return the BigDecimal object // that holds the value of the remainder remainder = b_dec2.remainder(b_dec3, ma_co); System.out.println("b_dec2.remainder(b_dec3,ma_co): " + remainder); }}

Output

输出量

remainder(BigDecimal): b_dec1.remainder(b_dec3): 0.12 remainder(BigDecimal,MathContext): b_dec2.remainder(b_dec3,ma_co): 0.55

翻译自:

转载地址:http://rotzd.baihongyu.com/

你可能感兴趣的文章
2017.10.30 天晴 昨天十公里没减肥
查看>>
Git 打标签(分布式版本控制系统)
查看>>
ASP.NET MVC:通过 FileResult 向 浏览器 发送文件
查看>>
CVE-2010-2883Adobe Reader和Acrobat CoolType.dll栈缓冲区溢出漏洞分析
查看>>
使用正确的姿势跨域
查看>>
AccountManager教程
查看>>
Android学习笔记(十一)——从意图返回结果
查看>>
算法导论笔记(四)算法分析常用符号
查看>>
HttpClient读取数据乱码的解决方案
查看>>
如何使用FireBug插件查询元素的xPath属性
查看>>
ultraedit激活
查看>>
总结(6)--- python基础知识点小结(细全)
查看>>
亿级曝光品牌视频的幕后设定
查看>>
ARPA
查看>>
JSP开发模式
查看>>
我的Android进阶之旅------>Android嵌入图像InsetDrawable的使用方法
查看>>
Detours信息泄漏漏洞
查看>>
win32使用拖放文件
查看>>
Android 动态显示和隐藏软键盘
查看>>
raid5什么意思?怎样做raid5?raid5 几块硬盘?
查看>>