Java BigInteger long

Example below illustrates the bigdecimal
class divide(BigDecimal divisor) method working. In the example method divides the
bigdecimal object value in which it is invoked by the bigdecimal object value passed inside its parentheses. The scale of the quotient is the scale of the value of object in which the
method is invoked minus the scale of the value of object specified. In short scale of quotient is
(this.object value scale) - (object.value scale). Moreover the result generated has to be stored by some bigdecimal object as this method returns
bigdecimal object type values only.
Method throws an Arithmetic Exception when a non-terminating quotient is generated. This means the quotient does not possess any termination point in to its fractional part. Method throws NumberFormatException if it
finds a value other than a integer or double value.
Syntax for using the method:
System.out.println(bigdecimal_objectName.divide(BigDecimal divisor));
or
BigDecimal x = this.object.divide(divisor);
Java_BigDecimal_divide_BigDecimal_Divisor.java
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.NumberFormat;
public class Java_BigDecimal_divide_BigDecimal_Divisor {
public static void main(String args[]) {
String str = "200", cv = "2";
BigInteger in = new BigInteger(str);
BigDecimal Divident_0 = new BigDecimal(in),
Divisor_0 = new BigDecimal(cv),
Quotient_0 = new BigDecimal(
Divident_0.divide(Divisor_0).toString()),
Remainder_0 = new BigDecimal(Integer.parseInt(str)
% Integer.parseInt(cv));
System.out.println("Quotient : " + Quotient_0 +
"\nRemainder : "+ Remainder_0);
// Similarly
double dear = 85.6, beloved = 70;
BigDecimal Divident_1 = new BigDecimal(dear),
Divisor_1 = new BigDecimal(beloved),
Quotient_1 = new BigDecimal(
Divident_1.divide(Divisor_1).toString()),
Remainder_1 = new BigDecimal(dear % beloved);
System.out.println("\nUnrounded values");
System.out.println("Quotient : " +
Quotient_1 + "\nRemainder : "+ Remainder_1);
NumberFormat Fraction = NumberFormat.getInstance();
Fraction.setMinimumFractionDigits(3);
System.out.println("\nRounded values");
System.out.println("Quotient : "
+ Fraction.format(Quotient_1)
+ "\nRemainder : " + Fraction.format(Remainder_1));
}
}
|
Download the code

|