您的位置:首页 > 编程语言 > Java开发

Java --计算百分比

2014-09-24 14:31 246 查看
Learn From: http://blog.csdn.net/maggiehexu/article/details/6387636

方法一:

public String getPercent(int x,int total){
String result = "";//接受百分比的值
double x_double = x*1.0;
double tempresult = x/total;

DecimalFormat df1 = new DecimalFormat("0.00%");    //##.00%   百分比格式,后面不足2位的用0补齐
result= df1.format(tempresult);

return result;
}


方法二:

public String getPercent(int x,int total){
String result = "";//接受百分比的值
double x_double = x*1.0;
double tempresult = x/total;

NumberFormat nf = NumberFormat.getPercentInstance();
nf.setMinimumFractionDigits(2);
result=nf.format(tempresult);

return result;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: