我正在为Android编写一个应用程序,我在将按钮上的浮动值作为文本时遇到了困难。我试过了:
float Percentage1 = (Level1/16)*100;
String s = Float.toString(Percentage1);
percent.setText(s+"%");
:
float Percentage1 = (Level1/16)*100f;
percent.setText(Float.toString(Percentage1));
按钮上总是显示0.0%。如果有人知道如何做到这一点,请回复!
(Level1/16)
将在整数算术中计算;也就是说,它的剩余部分将被丢弃。
补救,使用(Level1/16.0)*100;
。使用16.0
会导致表达式在浮点数中求值。