如何圆十进制数的机器人 [英] How to round decimal numbers in Android
本文介绍了如何圆十进制数的机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
双重价值,圆了在Java中
我收到浮点数作为输入,我希望它小数点后舍入到2位数。 也就是说,例如,如果我得到18.965518作为输入,我希望它是18.97。怎么办呢?
I am getting float number as input and I want it to round to 2 digits after decimal point. i.e. for example if I get 18.965518 as input, I want it to be 18.97. How to do it?
推荐答案
您可以使用<一个href="http://developer.android.com/reference/java/text/DecimalFormat.html"><$c$c>DecimalFormat$c$c>object,类似于常规的Java。
You can use the DecimalFormat
object, similar to regular Java.
尝试
double roundTwoDecimals(double d)
{
DecimalFormat twoDForm = new DecimalFormat("#.##");
return Double.valueOf(twoDForm.format(d));
}
(code例如,从<一解禁href="http://www.java-forums.org/advanced-java/4130-rounding-double-two-decimal-places.html">http://www.java-forums.org/advanced-java/4130-rounding-double-two-decimal-places.html)
这篇关于如何圆十进制数的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文