JSP学习笔记(三十八):Java获取数值百分比的函数
获取一个数值的百分比:
//获取一个double类型数值的百分比
//example: FormatPercent(0.33333333,2) = 33%
public String FormatPercent(double number,int newValue)
{
java.text.NumberFormat nf = java.text.NumberFormat.getPercentInstance();
nf.setMinimumFractionDigits(newValue);
return nf.format(number);
}
//example: FormatPercent(0.33333333,2) = 33%
public String FormatPercent(double number,int newValue)
{
java.text.NumberFormat nf = java.text.NumberFormat.getPercentInstance();
nf.setMinimumFractionDigits(newValue);
return nf.format(number);
}
相反,根据百分比获取数值:
// 获取数值
// example:FormatNumber("50%") = 0.5
public double FormatNumber(String value) {
double n = 0;
java.text.NumberFormat nf = java.text.NumberFormat.getPercentInstance();
try {
n = nf.parse(value).doubleValue();
} catch (ParseException e) {
}
return n;
}