舍入到小数点后一位?

问题描述:

我有以下变量初始化为用户输入,舍入到小数点后一位?

int completedPasses = reader1.nextInt();

int attemptedPasses = reader2.nextInt(); (250/3)*((已完成的程序/尝试的程序) - 0.3);

如何将上述计算舍入到小数点后一位的两倍?

+0

可以使用Math.round – hhafeez

+0

@hhafeez你就是不行。看到重复。 – EJP

+0

你不能。双打没有小数位。他们有二进制的地方。如果你想要小数位,你必须使用小数基数。请参阅'DecimalFormat'。 – EJP

使用的DecimalFormat:

 DecimalFormat df = new DecimalFormat("#.#"); 
    df.format(completionRatio); 

或者Math.round:

Math.round(completionRatio);