简体   繁体   English

是否有更好的方法在Android中显示%

[英]Is there a better way to display % in Android

Currently, I am displaying a value as a Percent by simply setting text as below: 目前,我正在通过简单地设置以下文本来将值显示为百分比:

mInterestRateEditText.setText(i + "%");

Is there a better way to do this? 有一个更好的方法吗? I have tried with DecimalFormat method like this: 我已经尝试过像这样的DecimalFormat方法:

private String displayPerc(int perc) {
     DecimalFormat df = new DecimalFormat("#%");
     return df.format(perc);
}

But for some reasons when i = 1 it displays 100%. 但是由于某些原因,当i = 1时,它将显示100%。

Is there a better way to display % other than concatenating the symbol at the end? 除了在末尾连接符号以外,还有没有更好的显示%的方法?

Thanks 谢谢

Declare a string in strings.xml 在strings.xml中声明一个字符串

<string name="percentage">%1$f %</string>

and set it in your code with 并在您的代码中设置

mInterestRateEditText.setText(getString(R.string.percentage, i));

EDIT : To do it the decimal format way, do 编辑:要以十进制格式进行操作,请执行

private String displayPerc(int perc) {
  DecimalFormat df = new DecimalFormat("#'%'");
  return df.format(perc);
} 

put '%' instead of %. 放'%'代替%。 Normal % will Multiply by 100 and show as percentage. 正常百分比将乘以100并显示为百分比。 Check this link http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html 检查此链接http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM