目前,我通过简单地设置如下文本来将值显示为百分比:
mInterestRateEditText.setText(i + "%");
有更好的方法吗?我试过像这样使用 DecimalFormat 方法:
private String displayPerc(int perc) {
DecimalFormat df = new DecimalFormat("#%");
return df.format(perc);
}
但由于某些原因,当 i = 1 时它显示 100%。
除了在末尾连接符号之外,是否有更好的显示 % 的方法?
谢谢
最佳答案
在strings.xml中声明一个字符串
<string name="percentage">%1$f %</string>
并在你的代码中设置它
mInterestRateEditText.setText(getString(R.string.percentage, i));
编辑:要以十进制格式进行操作,请执行
private String displayPerc(int perc) {
DecimalFormat df = new DecimalFormat("#'%'");
return df.format(perc);
}
用'%'代替%。正常百分比将乘以 100 并显示为百分比。检查此链接 http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html
关于java - 有没有更好的方法在 Android 中显示 %,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35934416/