This question already has answers here:
How to round a number to n decimal places in Java
                                
                                    (31个答案)
                                
                        
                                5年前关闭。
            
                    
我有

double a= = 13;
double b = 6;
double c = a/b;


我想确保它以三位小数返回。我试过了

Math.round(c); //gives me an error instead.

最佳答案

如果需要格式化输出,例如打印输出或在textview中显示,请使用:

DecimalFormat threeDecRound = new DecimalFormat();
threeDecRound.setMaximumFractionDigits(3);
threeDecRound.setRoundingMode(RoundingMode.HALF_DOWN);
threeDecRound.format(number)

10-04 14:16