2013-12-13 70 views
1

在我的ProgressBar中我想以0.0格式显示进度百分比。我有这个代码,但结果总是向上或向下舍入,而不显示小数点后的数字。Android百分比ProgressBar

double value = Double.valueOf(data); 
value = numero_a/numero_b *100; 
final float numero_float = (float) value; 
new Thread(new Runnable() { 
    float progressStatus = 0; 
    Handler handler = new Handler(); 
    public void run() { 
     while (progressStatus < numero_float) {  
      progressStatus += 1; 
      handler.post(new Runnable() { 
       public void run() { 
        tvPercentuale.setText(progressStatus+"%"); 
        mProgress.setProgress((int)progressStatus); 

回答

0
NumberFormat nf = NumberFormat.getPercentInstance(); 
    //this will make sure the format is in XX.X% 
    nf.setMaximumFractionDigits(1); 
    nf.setMinimumFractionDigits(1); 
    ..... 
    tvPercentuale.setText(nf.format(progressStatus)) 

编辑:

int max = mProgress.getMax(); 

for(int i=0; i<max; i++){ 
int progress = i; 
float progressValue = (float)i/(float)max; 
tvPercentuale.setText(nf.format(progressValue)) 
mProgress.setProgress(progress); 
} 
+0

问题是,即使如此,值也是四舍五入的。 我想看到47.8%。但现在我看到48.0% – user2996988

+0

然后progressStatus事先被舍入。也许你需要这样做:float progressStatus = 0F; .... progressStatus + = 1F; – runor49

+0

问题是'setProgress'不接受float数据,只接受int。 有没有办法接受float? – user2996988

0

增加了10倍的变量,因此:

while(progressStatus < numero_float * 10) 

10接着递增progressStatus:

progressStatus += 10; 

然后设置文本为:

tvPercentuale.setText("" + progressStatus/10.0f + "%"); 
mProgress.setProgress((int)(progressStatus/10)); 
+0

结果相同。四舍五入的结果(例如48.0%)应该是准确的,即47.8%。也许不支持小数? – user2996988