Java Program For Arithmetic Operations Between BigDecimal and Primitive Data Types
Last Updated :
22 Nov, 2021
The floating-point data types (float and double) are not as accurate to be used in the financial calculations. Therefore, Java offers a separate class "BigDecimal" to perform the operations and avoid the minimal chances of mistakes in calculations. BigDecimal class provides operations on double numbers for arithmetic, scale handling, rounding, comparison, format conversion, and hashing. It can handle very large and very small floating-point numbers with great precision but compensating with the time complexity a bit. BigDecimal provides numerous methods to be implemented upon. In this article, we'll be discussing the basic arithmetic operations which can be performed on BigDecimal and also performing the same operations between BigDecimal and primitive data types such as int, short, long, etc.
Example 1:
Java
// Java Program to Add, Subtract and Multiply
// Two big decimal numbers
// Importing input output classes
import java.io.*;
// Importing BigDecimal class from
// java.math package
import java.math.BigDecimal;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring two variables of type BigDecimal
// Custom numbers for illustration
BigDecimal num1 = new BigDecimal("15.3514628768");
BigDecimal num2 = new BigDecimal("45.37844");
// Mathematic operation over two numbers
// Addition using add() method
System.out.println("Addition of num1 and num2 = "
+ (num1.add(num2)));
// Subtraction using subtract() method
System.out.println("Subtraction of num1 and num2 = "
+ (num1.subtract(num2)));
// Multiplication using multiply() method
System.out.println(
"Multiplication of num1 and num2 = "
+ (num1.multiply(num2)));
}
}
OutputAddition of num1 and num2 = 60.7299028768
Subtraction of num1 and num2 = -30.0269771232
Multiplication of num1 and num2 = 696.625437067096192
Output Explanation:
Note that the accuracy of the operations is very high. Now performing division on the same set of numbers in the below example as follows:
Example 2:
Java
// Java Program to show exception thrown if blunt division
// of two big decimal numbers is carried on
// Importing input output classes
import java.io.*;
// Importing BigDecimal class from
// java.math package
import java.math.BigDecimal;
// Main class
class GFG {
// Main driver method
public static void main (String[] args) {
// Creating an object of bigDecimal class and
// initializing the big decimal values
// Custom entry
BigDecimal num1 = new BigDecimal("15.3514628768");
BigDecimal num2 = new BigDecimal("45.37844");
// Division over two numbers
// using divide() method and printing the resultant number
System.out.println("Division of num1 and num2 = " + (num2.divide(num1)));
}
}
Output:
Output explanation:
This error occurred because the division of the two numbers was non-terminating, and we know that BigDecimal is introduced to provide the maximum accuracy. Hence, it produces an error. We will be rectifying the same in the next code where we will be dividing the same numbers, but now the data type is double, and it should not produce any error and gives some answers.
Example 3:
Java
// Java Program to show division of two big decimal numbers
// Importing input output classes
import java.io.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating and initializing double numbers
// Custom entries
double num1 = 15.3514628768;
double num2 = 45.37844;
// Dividing two big decimal numbers after which
// numbers obtained will also be a big decimal
// number
// Print and display the resultant number
System.out.println("Division of num1 and num2 = "
+ (num2 / num1));
}
}
OutputDivision of num1 and num2 = 2.955968454874647
Until now, we've performed the arithmetic operations on two BigDecimal objects, and now we shall try to do the same thing with the primitive data types.So here we go.
Example 4:
Java
// Java Program to Add big decimal number
// with an Integer number
// Importing input output classes
import java.io.*;
// importing BigDecimal class from
// java.math package
import java.math.BigDecimal;
// main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring a BigDecimal object
BigDecimal num1 = new BigDecimal("12");
// Declaring an integer number
int num2 = 15;
// Adding a big decimal number with
// an integer number
System.out.println("Addition of num1 and num2 ="
+ num1.add(num2));
}
}
Output:
Output Explanation:
This is because BigDecimal only allows the operation to be performed only on BigDecimal objects. Therefore, we need to convert our primitive data type variable to the object of BigDecimal using the constructor of BigDecimal Class. The above conflict is resolved in the below code as follows.
Example 5:
Java
// Java Program to Add big decimal number
// with an Integer number
// Importing input output classes
import java.io.*;
// Importing BigDecimal class from
// java.math package
import java.math.BigDecimal;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring a BigDecimal object
BigDecimal num1 = new BigDecimal("12");
// Declaring an integer number
int num2 = 15;
// Print the addition of two numbers
System.out.println(
"Addition of num1 and num2 = "
+ num1.add(new BigDecimal(num2)));
}
}
OutputAddition of num1 and num2 = 27
Output Explanation:
What is happening here is that we are creating a new object of class BigDecimal with a value same as num2 and we are directly passing the object created through the constructor to the argument of add() method. And we get our required answer. The arithmetic operations between BigDecimal and int are shown below.
Example 6:
Java
// Importing all input output classes
import java.io.*;
// Importing BigDecimal class from
// java.math package
import java.math.BigDecimal;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring a BigDecimal object
BigDecimal num1 = new BigDecimal("12");
// Declaring an integer number
int num2 = 15;
// Addition over numbers
// Print the resultant desired number
System.out.println(
"Addition of num1 and num2 = "
+ num1.add(new BigDecimal(num2)));
// Subtraction over numbers
// Print the resultant desired number
System.out.println(
"Subtraction of num1 and num2 = "
+ num1.subtract(new BigDecimal(num2)));
// Multiplication over numbers
// Print the resultant desired number
System.out.println(
"Multiplication of num1 and num2 = "
+ num1.multiply(new BigDecimal(num2)));
// Division over numbers
// Print the resultant desired number
System.out.println(
"Division of num1 and num2 = "
+ num1.divide(new BigDecimal(num2)));
}
}
OutputAddition of num1 and num2 = 27
Subtraction of num1 and num2 = -3
Multiplication of num1 and num2 = 180
Division of num1 and num2 = 0.8
Similar Reads
Java Program to Illustrate Use of Binary Literals A binary literal is a number represented in binary (0s and 1s). In Java, you can use binary literals for integral types such as byte, short, int, and long. To specify a binary literal, prefix the number with 0b or 0B. This allows you to define numbers in binary format directly in your code. When exe
4 min read
Java Program to Illustrate the usage of Octal Integer Octal is a number system where a number is represented in powers of 8. So all the integers can be represented as an octal number. Also, all the digit in an octal number is between 0 and 7. In java, we can store octal numbers by just adding 0 while initializing. They are called Octal Literals. The da
2 min read
BigDecimal sqrt() Method in Java with Examples The java.math.BigDecimal.sqrt(MathContext mc) is an inbuilt function added in Java SE 9 & JDK 9 which returns BigDecimal value of square root of a BigDecimal on which sqrt() method is applied with rounding according to the context settings. Syntax: public BigDecimal sqrt(MathContext mc) Paramete
2 min read
Java Program to Convert int to long Given a number of int datatype in Java, your task is to write a Java program to convert this given int datatype into a long datatype. Example: Input: intnum = 5 Output: longnum = 5 Input: intnum = 56 Output: longnum = 56 Int is a smaller data type than long. Int is a 32-bit integer while long is a 6
2 min read
Java Program to Convert Double to Long Given a Double real number. Write a Java program to convert the given double number into a Long (long) in Java. Examples: Input: double = 2545.241 Output: 2545 Input: double = 21.54 Output: 21 Double data type: The double data type is a double-precision 64-bit IEEE 754 floating-point. Its value rang
4 min read
Java Program to Convert Int to Double Java data types can be categorized as primitive and non-primitive. Primitive data types contain a single value, whereas non-primitive data types contain an address of the variable value. Java supports 7 primitive data types - boolean, byte, char, short, int, long, float, and double. These data types
4 min read