
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Divide One BigDecimal from Another in Java
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
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
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
Advertisements