Multiplying a BigDecimal by an Integer in Java

06 May 2023 Balmiki Mandal 0 Core Java

Multiplying a BigDecimal by an Integer in Java

Java provides a powerful way to perform arithmetic operations, including multiplying a BigDecimal by an integer. Multiplying a BigDecimal is especially useful when dealing with large numeric values.

How to Multiply a BigDecimal by an Integer in Java

Multiplying a BigDecimal by an integer is a simple process using the BigDecimal class’s multiply() method. The following code demonstrates how to use it:

BigDecimal bd = new BigDecimal("100.00");
int x = 5;
BigDecimal result = bd.multiply(new BigDecimal(x));

System.out.println(result); // prints 500.00

In the above example, we used the BigDecimal constructor to create a BigDecimal with the value of 100.00. We then created an int variable called x and set its value to 5. We then used the BigDecimal multiply() method to multiply the BigDecimal bd by the int x, creating a new BigDecimal with the result of 500.00.

Using the BigDecimal multiply(BigDecimal multiplicand) Method

You can also use the BigDecimal multiply() method to multiply two BigDecimals. This method has the following syntax:

BigDecimal.multiply(BigDecimal multiplicand)

For example, if you wanted to multiply two BigDecimals, bd1 and bd2, with the values 10.00 and 20.00 respectively, you could do so using the following code:

BigDecimal bd1 = new BigDecimal("10.00");
BigDecimal bd2 = new BigDecimal("20.00");

BigDecimal result = bd1.multiply(bd2);

System.out.println(result); // prints 200.00

As you can see, the BigDecimal multiply() method can be used to multiply both BigDecimals and integers. It is an incredibly useful tool for performing large arithmetic operations in Java.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.