2012-07-09 18 views

回答

6

如果你只用正數時,您還可以使用int i =(INT)(d + 0.5)。

編輯:如果你想圓負數了(向正無窮大,這樣-5.4變爲-5,例如),你可以用這一點。如果你想四捨五入到更高的數值(舍入-5.4到-6),你最好建議使用另一個答案提出的其他函數。

20

使用標準舍入函數? Math.round()

還有Math.floor()Math.ceil(),這取決於你需要什麼。

2

Java提供Math類中的一些功能來做到這一點。對於你的情況,請嘗試Math.ceil(4.5)將返回5

6

您可以使用

int i = Math.round(d);

2
new BigDecimal(3.4); 
Integer result = BigDecimal.ROUND_HALF_UP; 

或者

Int i = (int)(202.22d); 
0

使用Math.max你可以做這樣的:

(int) Math.max(1, (long) Math.ceil((double) (34)/25) 

這會給你2