Метод BigDecimal остатка () в Java с примерами
Java.math.BigDecimal .remainder (делитель BigDecimal) используется для вычисления остатка от двух BigDecimals. Остаток дается this.subtract (this.divideToIntegralValue (делитель) .multiply (делитель)). Этот метод выполняет операцию над текущим BigDecimal, с помощью которого этот метод вызывается и BigDecimal передается в качестве параметра.
Примечание: это не операция по модулю (результат может быть отрицательным).
В Java доступны две перегрузки метода остатка, которые перечислены ниже:
- остаток (делитель BigDecimal)
- остаток (делитель BigDecimal, MathContext mc)
остаток (делитель BigDecimal)
Синтаксис:
общедоступный BigDecimal остаток (делитель BigDecimal)
Параметры: этот метод принимает делитель параметра, на который нужно разделить этот BigDecimal для получения остатка.
Возвращаемое значение: этот метод возвращает BigDecimal, содержащий результат (этот% делитель) .
Исключение: делитель параметра не должен быть равен 0, в противном случае возникает арифметическое исключение.
Below programs is used to illustrate the remainder() method of BigDecimal.
// Java program to demonstrate // remainder() method of BigDecimal import java.math.BigDecimal; public class GFG { public static void main(String[] args) { // BigDecimal object to store the result BigDecimal res; // For user input // Use Scanner or BufferedReader // Two objects of String created // Holds the values String input1 = "31452678569" ; String input2 = "2468" ; // Convert the string input to BigDecimal BigDecimal a = new BigDecimal(input1); BigDecimal divisor = new BigDecimal(input2); // Using remainder() method res = a.remainder(divisor); // Display the result in BigDecimal System.out.println(res); } } |
373
remainder(BigDecimal divisor, MathContext mc)
This method is used to calculate the remainder of two BigDecimals whose value is (this % divisor), with rounding according to the context settings. The MathContext settings affect the implicit divide used to compute the remainder. Therefore, the remainder may contain more than mc.getPrecision() digits.
Syntax:
public BigDecimal remainder(BigDecimal divisor, MathContext mc)
Parameters: This method accepts a parameter divisor by which this BigDecimal is to be divided and a parameter mc of type MathContext for context settings.
Return value: This method returns a BigDecimal which holds the result (this % divisor).
Exception: The method throws Arithmetic Exception for following conditions:
- The parameter divisor must not be 0.
- If mc.precision > 0 and the result requires a precision of more than mc.precision digits.
Below programs is used to illustrate the remainder() method of BigDecimal.
// Java program to demonstrate // remainder() method of BigDecimal import java.math.*; public class GFG { public static void main(String[] args) { // BigDecimal object to store the result BigDecimal res; // For user input // Use Scanner or BufferedReader // Two objects of String created // Holds the values String input1 = "24536482" ; String input2 = "264" ; // Convert the string input to BigDecimal BigDecimal a = new BigDecimal(input1); BigDecimal divisor = new BigDecimal(input2); // Set precision to 5 MathContext mc = new MathContext( 5 ); // Using remainder() method res = a.remainder(divisor, mc); // Display the result in BigDecimal System.out.println(res); } } |
58
References: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigDecimal.html#remainder(java.math.BigDecimal)
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.