2012-08-30 121 views
-1

我要四捨五入的數值爲前:四捨五入在JAVA

12.166666 ----> 12.00

12.49999 ----> 12.00

12.5111 ---- > 13.00

12.9999 ----> 13.00

我要四捨五入WRT 50派士。

+4

[谷歌](https://www.google.com/search?q=round+a+number+in+javascript&sugexp=chrome,mod=4&sourceid=chrome&ie=UTF-8)是你的朋友。 –

+0

檢查http://stackoverflow.com/questions/9902968/why-does-math-round0-49999999999999994-return-1 –

+0

的Java或Javascript? – RonK

回答

8

您可以查看Math.round(double a)方法。

System.out.println(Math.round(12.51));//Yields 13 
System.out.println(Math.round(12.49));//Yields 12 
System.out.println(Math.round(12.50));//Yields 13 
+0

中產生語法錯誤。 ');' – timidboy

+0

@Onchie:是的,你是對的,我的壞XD。現在應該修復。 – npinti

+0

這會導致[意外](http://stackoverflow.com/questions/9902968/why-does-math-round0-49999999999999994-return-1?lq=1)有時。 – Jesper