/*
* 浮点转百分比,保留2位小数
* 0.123456 -> 12.34%
*/
public static String floatToPercent(Float num) {
NumberFormat percentFormat = NumberFormat.getPercentInstance();
percentFormat.setMaximumFractionDigits(2); //最大小数位数
return percentFormat.format(num);
}
/*
* 字符串转百分比,保留2位小数
* 0.123456 -> 12.34%
*/
public static String floatToPercent(String num) {
NumberFormat percentFormat = NumberFormat.getPercentInstance();
percentFormat.setMaximumFractionDigits(2); //最大小数位数
if (StringUtils.isEmpty(num)) {
return "";
}
float num_float;
try {
num_float = Float.parseFloat(num);
} catch (Exception e) {
e.printStackTrace();
return "";
}
return percentFormat.format(num_float);
}