晚上好,Math.round不会四舍五入
我的代码不会四舍五入,我不知道为什么!请帮忙!
import java.util.*;
import java.math.*;
public class Arithmetic {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int tipPercentage; // tip percentage
int taxPercentage; // tax percentage
int totalTip; // total tip
int totalTax; // total tax
double mealCost; // original meal price
double totalCost = 0.0; // total meal price
// Write your calculation code here.
mealCost = scan.nextDouble();
tipPercentage = scan.nextInt();
taxPercentage = scan.nextInt();
totalCost += mealCost;
totalCost += mealCost*tipPercentage/100;
totalCost += mealCost*taxPercentage/100;
// cast the result of the rounding operation to an int and save it as totalCost
mealCost=(int)Math.round(totalCost);
// Print your result
System.out.println (" The total meal cost is " + totalCost + " dollars. ");
}
}
你的问题有点含糊不清,你能不能请编辑一下更详细的问题 –
你有一个错字,但整个方法是错误的。在重复的原因中查看我的答案。如果你想要小数位,你必须使用小数基数,例如通过'BigDecimal'或'DecimalFormat',在任何情况下你都应该使用'BigDecimal'来赚钱,而不是浮点。 – EJP