没什么多说的,贴代码
public static String setWinRate(int wc, int pc)
{
double percent;
if (pc != 0)
{
percent = (double) wc / (double) pc;
}
else
{
percent = 1.00D;
}
//获取格式化对象
NumberFormat rate = NumberFormat.getPercentInstance();
//设置百分数精确度2即保留两位小数
rate.setMinimumFractionDigits(2);
//最后格式化并输出
System.out.println("百分数 胜率:" + rate.format(percent));
return rate.format(percent);
}
wc是胜利的次数,pc是总共玩的次数, 需要导入包 - java.text.NumberFormat。