作为我的第一个项目,我正在为我的板球比赛编写一个统计程序。
我遇到的问题是在板球运动中,每轮有 6 个球,保龄球符号为“overs.ballnumber”。例如,如果我投了 4 轮并在新一轮中投了 3 个球,则符号将为 4.3
我需要知道的是,一旦到达第 6 个球,我将如何设置一个四舍五入到下一轮的数字。
例如4.6 轮应四舍五入为 5
最佳答案
以下程序演示了如何评估值的小数部分;
public class Test {
public static void main(String[] args) {
double roundedUp=Math.ceil(4.1);
System.out.println(shouldBeRoundedUp(2.2,0.5));
System.out.println(shouldBeRoundedUp(2.7,0.5));
}
public static boolean shouldBeRoundedUp(double value, double roundIfAbove){
double decimalPart=value%1; //this is the modulus operator, or remainder operator
if (decimalPart>roundIfAbove){
return true;
}else{
return false;
}
}
}
关于java - 客户数量四舍五入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19095836/