2012-08-30 120 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