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]: 0
 
divideAndRemainder(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 方法 示例

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐