java处理金额格式
public static String btos(BigDecimal amount) {
try {
String format = "#,###.00";
if (null == amount) {
//amount = new BigDecimal(0.00);
return null;
}
amount = amount.setScale(2, BigDecimal.ROUND_DOWN);
//-1:小于 0:等于 1:大于
//小于0 小于1 等于0
if (amount.compareTo(new BigDecimal("0")) == -1 || amount.compareTo(new BigDecimal("1")) == -1 || amount.compareTo(new BigDecimal("0")) == 0) {
format = "#,###0.00";
}
DecimalFormat dformat = new DecimalFormat(format);
return dformat.format(amount);
} catch (Exception e) {
e.printStackTrace();
}
return "0.00";
}
js处理(第一个参数是金额,第二个参数 n是小数点位数)
//格式化金额
function parseFormatNum(number, n) {
if (n != 0) {
n = (n > 0 && n <= 20) ? n : 2;
}
number = parseFloat((number + "").replace(/[^\d.-]/g, "")).toFixed(n) + "";
var sub_val = number.split(".")[0].split("").reverse();
var sub_xs = number.split(".")[1];
var show_html = "";
for (i = 0; i < sub_val.length; i++) {
show_html += sub_val[i] + ((i + 1) % 3 == 0 && (i + 1) != sub_val.length ? "," : "");
}
if (n == 0) {
return show_html.split("").reverse().join("");
} else {
return show_html.split("").reverse().join("") + "." + sub_xs;
}
}