Arithmetic Operations Between BigDecimal and Primitive Data Types in Java



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

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

Syntax

The syntax doubleValue() method returns the value of a BigDecimal object as a double primitive type.
double doubleNum1 = num1.doubleValue();

Where "num1" is a BigDecimal object.

What is BigDecimal in Java?

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.

What are primitive data types in Java?

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.

Methods for Arithmetic Operations Between BigDecimal and Primitives

The following are the different methods for performing arithmetic operations between BigDecimal and primitive data types.

Using BigDecimal Class Method

In this method, the BigDecimal object is initialized, and then, using the BigDecimal class method, arithmetic operations are performed between BigDecimal and Primitive Data Types.

  • Step 1. Initialize BigDecimal and Primitive Variables: We create a BigDecimal object (num1) initialized with a string value and a primitive int variable (num2). These variables will be used in arithmetic operations.
BigDecimal num1 = new BigDecimal("10.25");  // BigDecimal initialized with "10.25"
int num2 = 5; // Primitive int initialized with 5
  • Step 2. Convert int to BigDecimal using BigDecimal.valueOf(): This step involves converting the primitive int value (num2) into a BigDecimal object. The BigDecimal.valueOf() method is used for this conversion.
BigDecimal sum = num1.add(BigDecimal.valueOf(num2));  // Converts num2 to BigDecimal and adds it to num1
  • Step 3. Perform Arithmetic Operations: Using the BigDecimal class methods, we perform addition, subtraction, multiplication, and division between the BigDecimal and the primitive int variable.

Addition: The add() method of BigDecimal is used to add two BigDecimal values together.

BigDecimal sum = num1.add(BigDecimal.valueOf(num2));  // num1 + num2

Subtraction: The subtract() method subtracts one BigDecimal from another.

BigDecimal difference = num1.subtract(BigDecimal.valueOf(num2));  // num1 - num2

Multiplication: The multiply() method is used to multiply two BigDecimal values.

BigDecimal product = num1.multiply(BigDecimal.valueOf(num2));  // num1 * num2

Division: The divide() method divides one BigDecimal by another.

BigDecimal quotient = num1.divide(BigDecimal.valueOf(num2), 2, BigDecimal.ROUND_HALF_UP);// num1 / num2 with scale and rounding

Example

Below is a Java program for performing arithmetic operations between BigDecimal and primitive data types using BigDecimal Class Methods.

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

Using Conversion between BigDecimal and Primitive Data Types

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

  • Step 1. Initialize BigDecimal and Convert to Primitive: A BigDecimal object is initialized and converted to a double using doubleValue(). An int variable is also initialized.
double doubleNum1 = num1.doubleValue();
double sum = doubleNum1 + num2; double difference = doubleNum1 - num2; double product = doubleNum1 * num2; double quotient = doubleNum1 / num2;
  • Step 2. Perform Arithmetic Operations: Arithmetic operations (addition, subtraction, multiplication, division) are performed between the double and int.
  • Step 3. Convert Results to BigDecimal: The arithmetic operations' results, which are in double format, are converted back to BigDecimal using the BigDecimal constructor.
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));

Example

Below is a Java program for performing arithmetic operations between BigDecimal and primitive data types using conversion between BigDecimal and primitive data types.

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
Updated on: 2025-01-23T23:07:25+05:30

229 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements