在 JAVA 中四舍五入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12190845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:57:37  来源:igfitidea点击:

Round off in JAVA

java

提问by sagarg

I want to round off a value for ex:

我想为 ex 舍入一个值:

12.166666 ----> 12.00

12.49999 ----> 12.00

12.5111 ----> 13.00

12.9999 ----> 13.00

12.166666 ----> 12.00

12.49999 ----> 12.00

12.5111 ----> 13.00

12.9999 ----> 13.00

I want to to round off wrt 50 paise.

我想四舍五入wrt 50 paise。

回答by npinti

You can take a look at the Math.round(double a)method.

你可以看看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

回答by Juan Mendes

You can use Math.round. For documentation take a look at: http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#round(double)

您可以使用Math.round. 有关文档,请查看:http: //docs.oracle.com/javase/7/docs/api/java/lang/Math.html#round(double)