如何在Java中将BigDecimal乘以整数 [英] How to multiply a BigDecimal by an integer in Java
本文介绍了如何在Java中将BigDecimal乘以整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Java中将BigDecimal乘以整数?我试过这个但不正确。
How do you multiply a BigDecimal by an integer in Java? I tried this but its not correct.
import java.math.BigDecimal;
import java.math.MathContext;
public class Payment {
int itemCost;
int totalCost = 0;
public BigDecimal calculateCost(int itemQuantity,BigDecimal itemPrice){
itemCost = itemPrice.multiply(itemQuantity);
totalCost = totalCost + itemCost;
return totalCost;
}
推荐答案
你有很多<您的代码中的em> type-mismatches ,例如尝试放置 int
值,其中 BigDecimal
是必需的。更正后的代码版本:
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(new BigDecimal(itemQuantity));
totalCost = totalCost.add(itemCost);
return totalCost;
}
}
这篇关于如何在Java中将BigDecimal乘以整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文