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 BigDecimal
import 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.12remainder(BigDecimal,MathContext):
b_dec2.remainder(b_dec3,ma_co): 0.55
翻译自: https://www.includehelp.com/java/bigdecimal-remainder-method-with-example.aspx