Android百分比字段填充过多

Android百分比字段填充过多,android,Android,在我的android应用程序中,我找到了另外两个计数器的百分比 使用此代码 double total1 = counter + counter1; double totaal2 = counter / total1 * 100.0; percentfield.setText("" + total2); 当百分比字段显示50%、25%等时,我的代码非常有效。 但它显示出33.33333% 有没有办法缩短我的百分比字段,只显示33.3 编辑 我在layout.xml下用这

在我的android应用程序中,我找到了另外两个计数器的百分比 使用此代码

double total1 =  counter + counter1;
double totaal2 = counter / total1 * 100.0;
            percentfield.setText("" + total2);
当百分比字段显示50%、25%等时,我的代码非常有效。 但它显示出33.33333% 有没有办法缩短我的百分比字段,只显示33.3

编辑 我在layout.xml下用这段代码将其缩短

android:maxLength="3" 
试试这个:

    totaal2  = totaal2  * 10;
    totaal2  = Math.round(totaal2 );
    totaal2  = totaal2  / 10;

这可以通过多种方式来实现。更好地使用这种方法,我发现这是一个漫长的过程,它对我很有效:

public static double round(double unrounded, int precision, int roundingMode)
{
BigDecimal bd = new BigDecimal(unrounded);
BigDecimal rounded = bd.setScale(precision, roundingMode);
return rounded.doubleValue();
}

e、 g,四舍五入(你的数字,3,大十进制,向上四舍五入)

使用DecimalFormat将双精度格式设置为字符串格式

e、 g

请参阅有关DecimalFormat的文档:

double total1 =  counter + counter1;
double totaal2 = counter / total1 * 100.0;

DecimalFormat formatter = new DecimalFormat("#0.0");
percentfield.setText(formatter.format(total2));