Skip to content

Instantly share code, notes, and snippets.

@jimbray
Created November 5, 2014 01:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimbray/5c5faad771ccb63b6474 to your computer and use it in GitHub Desktop.
Save jimbray/5c5faad771ccb63b6474 to your computer and use it in GitHub Desktop.
金额格式化
/**
* 金额格式化
* @param s 金额
* @param len 小数位数
* @return 格式后的金额
*/
public static String FormatCurrency(String s, int len) {
if (s == null || s.length() < 1) {
return "0.00";
}
NumberFormat formater = null;
double num = Double.parseDouble(s);
StringBuffer buff = new StringBuffer();
if (len == 0) {
formater = new DecimalFormat("###,###");
} else {
buff.append("###,###.");
for (int i = 0; i < len; i++) {
buff.append("#");
}
formater = new DecimalFormat(buff.toString());
}
formater.setMinimumFractionDigits(len);//设置最小存在的小数点后位数
return formater.format(num).toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment