【问题标题】:Percentage Format in settext not correctly displayedsettext 中的百分比格式未正确显示
【发布时间】:2016-11-28 12:46:07
【问题描述】:

我编写了一个小测试,最后结果应该显示一个百分比。目前有三个问题,百分比几乎是正确的,例如。 2/3 将显示 66.0% 而不是 66.7 或 67。 百分比格式为双精度,分数格式为整数。这可能是一个原因吗? 我也尝试过在 settext 中进行计算,但这不起作用。因此我发明了百分比变量。

...
 else{
                //neue Frage oder Testende
                if(durchlauf < 3){
                    SetNextQuestion();
                }
                else{
                    qt.setText("");
                    rdg.setVisibility(View.INVISIBLE);
                    hintbutton.setVisibility(View.INVISIBLE);

                    percentage = ((score * 100)  / 3 );
                    t.setText("Test abgeschlossen \n" + "Punkte: " + score + " (" + String.format("%.2f", percentage) + "%)");
                }

【问题讨论】:

标签: android string format settext


【解决方案1】:
percentage = ((score * 100)  / 3 );

此赋值的右侧计算为整数,然后才将整数结果分配给双精度数。您可能还想将计算更改为浮点数:

percentage = ((score * 100.0)  / 3 );

【讨论】:

  • 谢谢 - 我现在明白了。有趣的是,计算显示的是 66 而不是 67,但我猜是整数 - 计算会删除逗号之后的所有内容而不是舍入。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-19
  • 1970-01-01
  • 2021-01-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多