Syntax error Java Program to divide one BigDecimal from another BigDecimal

Java Program to divide one BigDecimal from another BigDecimal



Use the divide method to divide one BigDecimal to another in Java. The method returns a BigDecimal whose value is (this / divisor), and whose preferred scale is (this.scale() - divisor.scale()).If the exact quotient cannot be represented (because it has a non-terminating decimal expansion) an ArithmeticException is thrown.

The following is an example −

Example

 Live Demo

import java.math.BigDecimal;
public class Demo {
   public static void main(String[] argv) throws Exception {
      BigDecimal val1 = new BigDecimal("37578975587");
      BigDecimal val2 = new BigDecimal("62567875598");
      System.out.println("Value 1 : "+val1);
      System.out.println("Value 2 : "+val2);
      // division
      val2 = val2.divide(val1,BigDecimal.ROUND_UP);
      System.out.println("Result (Division) = "+val2);
   }
}

Output

Value 1 : 37578975587
Value 2 : 62567875598
Result (Division) = 2

Let us see another example −

Example

 Live Demo

import java.math.*;
public class Demo {
   public static void main(String[] args) {
      BigDecimal bg1, bg2, bg3;
      bg1 = new BigDecimal("99");
      bg2 = new BigDecimal("11");
      bg3 = bg1.divide(bg2);
      String str = "Division = " +bg3;
      System.out.println( str );
   }
}

Output

Division = 9
Updated on: 2019-07-30T22:30:24+05:30

175 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements