You have a lot of type-mismatches in your code such as trying to put an int value where BigDecimal is required. The corrected version of your code:
public class Payment
{
BigDecimal itemCost = BigDecimal.ZERO;
BigDecimal totalCost = BigDecimal.ZERO;
public BigDecimal calculateCost(int itemQuantity, BigDecimal itemPrice)
{
itemCost = itemPrice.multiply(BigDecimal.valueOf(itemQuantity));
totalCost = totalCost.add(itemCost);
return totalCost;
}
}
Answer from Juvanis on Stack Overflow Top answer 1 of 3
108
You have a lot of type-mismatches in your code such as trying to put an int value where BigDecimal is required. The corrected version of your code:
public class Payment
{
BigDecimal itemCost = BigDecimal.ZERO;
BigDecimal totalCost = BigDecimal.ZERO;
public BigDecimal calculateCost(int itemQuantity, BigDecimal itemPrice)
{
itemCost = itemPrice.multiply(BigDecimal.valueOf(itemQuantity));
totalCost = totalCost.add(itemCost);
return totalCost;
}
}
2 of 3
4
If I were you, I would set the scale of the BigDecimal so that I dont end up on lengthy numbers. The integer 2 in the BigDecimal initialization below sets the scale.
Since you have lots of mismatch of data type, I have changed it accordingly to adjust.
class Payment
{
BigDecimal itemCost=new BigDecimal(BigInteger.ZERO, 2);
BigDecimal totalCost=new BigDecimal(BigInteger.ZERO, 2);
public BigDecimal calculateCost(int itemQuantity,BigDecimal itemPrice)
{
BigDecimal itemCost = itemPrice.multiply(new BigDecimal(itemQuantity));
return totalCost.add(itemCost);
}
}
BigDecimals are Object , not primitives, so make sure you initialize itemCost and totalCost , otherwise it can give you nullpointer while you try to add on totalCost or itemCost
Tutorialspoint
tutorialspoint.com › java › math › bigdecimal_multiply_mc.htm
Java.math.BigDecimal.multiply() Method
The java.math.BigDecimal.multiply(BigDecimal multiplicand, MathContext mc) returns a BigDecimal whose value is (this × multiplicand), with rounding according to the context settings.
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.math.bigdecimal.multiply
BigDecimal.Multiply Method (Java.Math) | Microsoft Learn
Java.Math · Assembly: Mono.Android.dll · Important · Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here. Returns a BigDecimal whose value is (this × multiplicand), and whose scale is (this.scale() + multiplicand.scale()).
Oracle
docs.oracle.com › javase › 8 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 8 )
October 20, 2025 - Returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal's unscaled value by the appropriate power of ten to maintain its overall value.
Tutorialspoint
tutorialspoint.com › home › java/math › java bigdecimal multiply
Java BigDecimal Multiply
September 1, 2008 - public BigDecimal multiply(BigDecimal multiplicand)
IncludeHelp
includehelp.com › java › bigdecimal-multiply-method-with-example.aspx
Java BigDecimal multiply() Method with Example
multiply(BigDecimal m_val, MathContext ma_co) method is used to get a BigDecimal that holds the value multiplied this BigDecimal by the given BigDecimal based on the given MathContext settings.
TutorialsPoint
tutorialspoint.com › multiply-one-bigdecimal-to-another-bigdecimal-in-java
Multiply one BigDecimal to another BigDecimal in Java
Use the multiply() method to multiply one BigDecimal to another in Java. This method returns a BigDecimal whose value is (this × multiplicand), and whose scale is (this.scale() + multiplicand.scale()). The following is an example &minu
Oracle
docs.oracle.com › en › java › javase › 23 › docs › api › java.base › java › math › BigDecimal.html
BigDecimal (Java SE 23 & JDK 23)
October 17, 2024 - A BigDecimal consists of an arbitrary ... the number of digits to the right of the decimal point. If the scale is negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale....
Jackrutorial
jackrutorial.com › 2018 › 06 › multiply-bigdecimal-by-int-in-java.html
Multiply BigDecimal by int in Java - Java Examples code - Learning to Write code for Beginners with Tutorials
The following example shows you how to multiply BigDecimal Object by int. package com.jackrutorial; import java.math.BigDecimal; import java.math.BigInteger; public class MultiplyBigdecimalByIntExample { public static void main(String[] args) { //price = $22.19 BigDecimal productPrice = new BigDecimal("22.19"); System.out.println("Product Cost = $" + productPrice); //Quantity = 2 int quantity = 2; System.out.println("Quantity = " + quantity); //Amount BigDecimal amount = new BigDecimal(BigInteger.ZERO, 2); BigDecimal totalCost = productPrice.multiply(new BigDecimal(quantity)); amount = amount.add(totalCost); System.out.println("---------------------"); System.out.println("Amout = $" + amount); } } In the code snippet above, we calculate the cost of product by multiplying the cost of each item with quantity (totalCost = productPrice*quantity).
Blogger
javablogx.blogspot.com › 2015 › 09 › how-to-multiply-bigdecimal-by-integer.html
Java Examples: How to multiply a BigDecimal by an integer in Java?
Multiplication with BigDecimals. ... import java.math.*; public class BigDecimalTest { public static void main(String[] args) { BigDecimal bd = new BigDecimal(2.0); int x = 5; bd = bd.multiply(new BigDecimal(x)); System.out.println(bd); } }
Simplesolution
simplesolution.dev › java-code-examples › java.math.bigdecimal.multiply
How to Multiply two BigDecimal values in Java
package simplesolution.dev; import java.math.BigDecimal; public class BigDecimalMultiplyExamples { public static void main(String... args) { BigDecimal bigDecimal1 = new BigDecimal("30.01"); BigDecimal bigDecimal2 = new BigDecimal("10.2"); BigDecimal result = bigDecimal1.multiply(bigDecimal2); System.out.println(bigDecimal1); System.out.println(bigDecimal2); System.out.println(result); } }
Oracle
docs.oracle.com › javase › 7 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 7 )
Returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal's unscaled value by the appropriate power of ten to maintain its overall value.
Oracle
docs.oracle.com › en › java › javase › 24 › docs › api › java.base › java › math › BigDecimal.html
BigDecimal (Java SE 24 & JDK 24)
July 15, 2025 - Returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal's unscaled value by the appropriate power of ten to maintain its overall value.
Electro4u
electro4u.net › blog › multiply-a-bigdecimal-by-an-integer-in-java-1316
Multiply a BigDecimal by an Integer in Java
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 ...
Oracle
docs.oracle.com › en › java › javase › 18 › docs › api › java.base › java › math › BigDecimal.html
BigDecimal (Java SE 18 & JDK 18)
August 18, 2022 - Returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal's unscaled value by the appropriate power of ten to maintain its overall value.
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › math › BigDecimal.html
BigDecimal (Java SE 21 & JDK 21)
January 20, 2026 - Returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal's unscaled value by the appropriate power of ten to maintain its overall value.