package com.test.coding.theme; import java.text.NumberFormat; import java.util.HashMap; import java.util.Map; public class SimpleMoneyFormat { public static final String EMPTY = ""; public static final String ZERO = "零"; public static final String ONE = "壹"; public static final String TWO = "贰"; public static final String THREE = "叁"; public static final String FOUR = "肆"; public static final String FIVE = "伍"; public static final String SIX = "陆"; public static final String SEVEN = "柒"; public static final String EIGHT = "捌"; public static final String NINE = "玖"; public static final String TEN = "拾"; public static final String HUNDRED = "佰"; public static final String THOUSAND = "仟"; public static final String TEN_THOUSAND = "万"; public static final String HUNDRED_MILLION = "亿"; public static final String YUAN = "元"; public static final String JIAO = "角"; public static final String FEN = "分"; public static final String DOT = "."; private static SimpleMoneyFormat formatter = null; private NumberFormat numberFormat = NumberFormat.getInstance(); private Map<String, String> chineseMap = new HashMap<String, String>(); private Map<Integer, String> unitMap = new HashMap<Integer, String>(); public SimpleMoneyFormat() { numberFormat.setGroupingUsed(false); // 不使用逗号分隔如 100,100,1 chineseMap.put("0", ZERO); chineseMap.put("1", ONE); chineseMap.put("2", TWO); chineseMap.put("3", THREE); chineseMap.put("4", FOUR); chineseMap.put("5", FIVE); chineseMap.put("6", SIX); chineseMap.put("7", SEVEN); chineseMap.put("8", EIGHT); chineseMap.put("9", NINE); chineseMap.put(DOT, DOT); unitMap.put(1, TEN); unitMap.put(2, HUNDRED); unitMap.put(3, THOUSAND); unitMap.put(4, TEN_THOUSAND); unitMap.put(5, TEN); unitMap.put(6, HUNDRED); unitMap.put(7, THOUSAND); unitMap.put(8, HUNDRED_MILLION); } public static SimpleMoneyFormat getInstance() { if (null == formatter) { formatter = new SimpleMoneyFormat(); } return formatter; } public String convertToChinese(String moneyStr) { StringBuffer sb = new StringBuffer(EMPTY); for (int i = 0; i < moneyStr.length(); i++) { sb.append(chineseMap.get(moneyStr.substring(i, i + 1))); } int indexOfDot = sb.indexOf(DOT); int length = indexOfDot != -1 ? indexOfDot - 1 : sb.length() - 1; int cursor = 1; for (int i = length; i > 0; i--) { sb.insert(i, unitMap.get(cursor)); if (cursor == 8) { cursor = 1; } else { cursor = cursor + 1; } } String dotPart = EMPTY; if (sb.indexOf(DOT) != -1) { dotPart = sb.substring(sb.indexOf(DOT)); sb.delete(sb.indexOf(DOT), sb.length()); } while (sb.indexOf("零拾") != -1) { sb.replace(sb.indexOf("零拾"), sb.indexOf("零拾") + 2, ZERO); } while (sb.indexOf("零佰") != -1) { sb.replace(sb.indexOf("零佰"), sb.indexOf("零佰") + 2, ZERO); } while (sb.indexOf("零仟") != -1) { sb.replace(sb.indexOf("零仟"), sb.indexOf("零仟") + 2, ZERO); } while (sb.indexOf("零万") != -1) { sb.replace(sb.indexOf("零万"), sb.indexOf("零万") + 2, TEN_THOUSAND); } while (sb.indexOf("零亿") != -1) { sb.replace(sb.indexOf("零亿"), sb.indexOf("零亿") + 2, HUNDRED_MILLION); } while (sb.indexOf("零零") != -1) { sb.replace(sb.indexOf("零零"), sb.indexOf("零零") + 2, ZERO); } if (sb.lastIndexOf(ZERO) == sb.length() - 1) { sb.delete(sb.length() - 1, sb.length()); } if (EMPTY.equals(dotPart) || DOT.equals(dotPart)) { dotPart = DOT + ZERO + ZERO; } else if (dotPart.length() == 2) { dotPart = dotPart + ZERO; } else if (dotPart.length() > 3) { dotPart = dotPart.substring(0, 3); // 只取小数点后前两位 } sb.append(dotPart); return sb.toString(); } public String addUnit(String str) { StringBuffer sb = new StringBuffer(str); int indexOfDot = sb.indexOf(DOT); if (indexOfDot != -1) { sb.replace(indexOfDot, indexOfDot + 1, YUAN); sb.insert(sb.length() - 1, JIAO); sb.insert(sb.length(), FEN); } else { sb.append(YUAN); } while (sb.indexOf("零角") != -1) { sb.replace(sb.indexOf("零角"), sb.indexOf("零角") + 2, EMPTY); } while (sb.indexOf("零分") != -1) { sb.replace(sb.indexOf("零分"), sb.indexOf("零分") + 2, EMPTY); } return sb.toString(); } public String format(String money) { String result = convertToChinese(money); result = addUnit(result); return result; } public String format(double d) { return format(numberFormat.format(d)); } public String format(int i) { return format(numberFormat.format(i)); } public String format(long l) { return format(numberFormat.format(l)); } public String format(Number number) { return format(numberFormat.format(number)); } public static void main(String[] args) { SimpleMoneyFormat format = SimpleMoneyFormat.getInstance(); System.out.println(format.format(new Double(2300000044324.02))); } }
输出结果为:
贰万叁仟亿零肆万肆仟叁佰贰拾肆元贰分