don*_*don 10 java integer rounding
如何将十进制数舍入为整数.
3.50 => 4 4.5 => 5 3.4 => 3
3.50 => 4
4.5 => 5
3.4 => 3
你是如何用Java做到的?谢谢!
Oli*_*rth 24
具有标准的舍入功能? Math.round()
Math.round()
还有Math.floor()和Math.ceil(),这取决于你所需要的.
Math.floor()
Math.ceil()
Eri*_*ner 10
您可以使用
int i = Math.round(f); long l = Math.round(d);
其中f和d是类型float和double分别.
f
d
float
double
Cos*_*ter 9
如果你只使用正数,你也可以使用int i =(int)(d + 0.5).
编辑:如果你想向上舍入负数(向正无穷大,例如-5.4变为-5),你也可以使用它.如果你想要舍入到更高的幅度(舍入-5.4到-6),你最好使用另一个答案提出的其他功能.
归档时间:
13 年,2 月 前
查看次数:
26762 次
最近记录:
7 年 前