Zba*_*ian 0 java decimal rounding
我不知道为什么,但如果我将参数从小数改为分数,我会得到两个不同的结果.
这些方法将返回确切的值.如果它是小数,我正在尝试对一个数字进行舍入,例如:
0.0 - > 0
0.1 - > 1
0.4 - > 1
0.5 - > 1
0.6 - > 1
1.0 - > 1
1.1 - > 2
// accepts Double
private void myRound(Double d){
int res = (int)Math.ceil(d);
return (res <= 0 ? 1 : res);
}
// acepts int
private void myRound(int i){
int res = (int)Math.ceil(i);
return (res <= 0 ? 1 : res);
}
Run Code Online (Sandbox Code Playgroud)
例:
System.out.println(myRound(14 / 10));
Run Code Online (Sandbox Code Playgroud)
输出:1
System.out.println(myRound(1.4);
Run Code Online (Sandbox Code Playgroud)
输出:2
问题是,转换首先转换为Integer,其中(14/10)为1,然后将其转换为1. 1.4是双倍,因此它将其作为双数字.