2015-04-23 66 views
-5

十進制數字,我需要舍入邏輯在以下pattern..for 2.23應該是2.2,爲2.26應該是2.3 ...請大家幫幫忙舍入邏輯在Java

+0

什麼是約2.25? – rpc1

+0

for 2.25它應該是2.3 –

+2

歡迎來到SO:SE。該網站的原則是在用戶表明自己努力解決問題後幫助用戶。這樣的努力可能是通過關鍵詞「java rounding」在線搜索。請參閱[問]以改善您的帖子。 – mins

回答

1
double a = <ur NUmber>; 
double roundOff = (double) Math.round(a*10)/10; 

希望這會幫助你。這裏2.25將是整數到2.3

0

如果你要打印,比使用printf方法,它具有默認舍入。

System.out.printf("%.1f", 2.23); 
System.out.printf("%.1f", 2.26); 

如果您需要四捨五入進行計算值比使用Math.round

double newValue= (double)Math.round(value*10)/10;