Java Program for Arithmetic Operations Between BigDecimal and Primitive Data Types


BigDecimal is a class in Java's java.math package that provides arbitrary-precision decimal arithmetic. It allows precise decimal calculations without the loss of precision that can occur with floating-point arithmetic. BigDecimal objects are immutable, so any arithmetic operation on them creates a new object with the result of the operation.

Primitive data types in Java are the basic building blocks of data in the language. They include int, double, Boolean, char, and others, and represent simple values like integers, floating-point numbers, and boolean values. They are stored directly in memory and are not objects, so they have lower memory overhead than objects.

Here, we will explore how to perform arithmetic operations between BigDecimal and Primitive Data Types.

Let's start!

For instance

Addition: The sum of a BigDecimal object with a value of 10.50 and an int primitive with a value of 5 is a BigDecimal object with a value of 15.50.

10.50 + 5 = 15.50

Algorithm

Algorithm-1

Step-1: A BigDecimal object num1 is initialized with a value, and an int variable num2 is also initialized with a value.

Step-2: The BigDecimal methods add(), subtract(), multiply(), and divide() are called to perform arithmetic operations between num1 and num2.

Step-3: The results of each arithmetic operation are stored in a separate BigDecimal object.

Step-4: The results are printed to the console using the println method.

Step-5: Then the program terminates.

Algorithm-2

Step-1: A BigDecimal object num1 is initialized with a value, and an int variable num2 is also initialized with a value.

Step-2: The doubleValue method is called on num1 to convert it to a double, and arithmetic operations are performed between the resulting double and num2.

Step-3: The results of each arithmetic operation are stored in a primitive double variable.

Step-4: The primitive doubles are converted back to BigDecimal objects using the BigDecimal constructor that takes a String argument.

Step-5: The results are printed to the console using the println method, and the program terminates.

Syntax

The doubleValue() method returns the value of a BigDecimal object as a double primitive type.

Below refers to the syntax of it-

double doubleNum1 = num1.doubleValue();

Where, “num1” is a BigDecimal obeject.

Multiple Approaches

We have provided the solutions in different approaches.

  • Using BigDecimal Class Methods

  • Using Conversion Between BigDecimal and Primitive Data types

Let's see the program along with its output one by one.

Approach-1: Using BigDecimal Class Methods

In this approach, the BigDecimal object is initialised and then by using BigDecimal class method performs arithmetic operations between BigDecimal and Primitive Data Types.

Example

import java.math.BigDecimal;

public class Main {
   public static void main(String[] args) {
      
      // Initialize a BigDecimal object and a primitive int variable
      BigDecimal num1 = new BigDecimal("10.25");
      int num2 = 5;
      
      // Perform arithmetic operations between BigDecimal and primitive data types
      BigDecimal sum = num1.add(BigDecimal.valueOf(num2));
      BigDecimal difference = num1.subtract(BigDecimal.valueOf(num2));
      BigDecimal product = num1.multiply(BigDecimal.valueOf(num2));
      BigDecimal quotient = num1.divide(BigDecimal.valueOf(num2));
      
      // Print results
      System.out.println("Sum: " + sum);
      System.out.println("Difference: " + difference);
      System.out.println("Product: " + product);
      System.out.println("Quotient: " + quotient);
   }
}

Output

Sum: 15.25
Difference: 5.25
Product: 51.25
Quotient: 2.05

Approach-2: Using Conversion between BigDecimal and Primitive Data Types

In this approach, the BigDecimal object is initialised and then by using conversion between BigDecimal and primitive data types performs arithmetic operations between BigDecimal and Primitive Data Types.

Example

import java.math.BigDecimal;

public class Main {
   public static void main(String[] args) {
      
      // Initialize a BigDecimal object and a primitive int variable
      BigDecimal num1 = new BigDecimal("20.55");
      int num2 = 7;
      
      // Convert the BigDecimal object to a double and perform arithmetic operations between primitive data types
      double doubleNum1 = num1.doubleValue();
      double sum = doubleNum1 + num2;
      double difference = doubleNum1 - num2;
      double product = doubleNum1 * num2;
      double quotient = doubleNum1 / num2;
      
      // Convert the result of each arithmetic operation back to a BigDecimal object
      BigDecimal sumBD = new BigDecimal(Double.toString(sum));
      BigDecimal differenceBD = new BigDecimal(Double.toString(difference));
      BigDecimal productBD = new BigDecimal(Double.toString(product));
      BigDecimal quotientBD = new BigDecimal(Double.toString(quotient));
      
      // Print results
      System.out.println("Sum: " + sumBD);
      System.out.println("Difference: " + differenceBD);
      System.out.println("Product: " + productBD);
      System.out.println("Quotient: " + quotientBD);
   }
}

Output

Sum: 27.55
Difference: 13.55
Product: 143.85
Quotient: 2.935714285714286

In this article, we explored how to perform arithmetic operations between BigDecimal and Primitive data types by using Java programming language.

Updated on: 17-Aug-2023

91 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements