字符串金额千分符格式化
一、代码
public class UnitTest {
// 金额字符串格式化为千分符
public static String fmtMicrometer(String amount) {
DecimalFormat df = null;
if (amount.indexOf(".") > 0) {
if (amount.length() - amount.indexOf(".") - 1 == 0) {
df = new DecimalFormat("###,##0.");
} else if (amount.length() - amount.indexOf(".") - 1 == 1) {
df = new DecimalFormat("###,##0.0");
} else {
df = new DecimalFormat("###,##0.00");
}
} else {
df = new DecimalFormat("###,##0");
}
double number = 0.0;
try {
number = Double.parseDouble(amount);
} catch (Exception e) {
number = 0.0;
}
return df.format(number);
}
public static String amountByYuan(Long amountByFen){
// 将 分 转换为 元
// 2f 保留两位小数 同理 3f 代表保留三位小数
String amountByYuan = String.format("%.3f", amountByFen.doubleValue() / 100);
// 将金额转换为 千分符 格式
String result = fmtMicrometer(amountByYuan);
return result;
}
// 测试
public static void main(String[] args) {
Long amount = 99822124l;
System.out.println(amountByYuan(amount));
}
}
二、测试结果


浙公网安备 33010602011771号