2013-07-31 29 views
0

在我的Android應用我發現的其他2個計數器 的百分比與此代碼安卓%的投籃欄填寫太多

double total1 = counter + counter1; 
double totaal2 = counter/total1 * 100.0; 
      percentfield.setText("" + total2); 

我的代碼工作的偉大,當百分比領域正顯示出50%,25%等。 但其顯示33.33333333333% 有沒有一種方法,以短了我的百分之領域也只顯示33.3

編輯 我有短下來與下layout.xml

驗證碼
android:maxLength="3" 

回答

0

試試這個:

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

它可以用很多方式來完成。更好地使用這種方法,我發現這個長後,它的工作對我來說:

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

例如,圓形(yourNumber,3,BigDecimal.ROUND_HALF_UP);

0

使用DecimalFormat將您的雙打格式化爲字符串。

例如

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

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

參見DecimalFormat的文檔: Decimal Format JavaDoc