public class SimpleMoneyFormat { private static final String EMPTY = ""; private static final String ZERO = "零"; private static final String ONE = "壹"; private static final String TWO = "贰"; private static final String THREE = "叁"; private static final String FOUR = "肆"; private static final String FIVE = "伍"; private static final String SIX = "陆"; private static final String SEVEN = "柒"; private static final String EIGHT = "捌"; private static final String NINE = "玖"; private static final String TEN = "拾"; private static final String HUNDRED = "佰"; private static final String THOUSAND = "仟"; private static final String TEN_THOUSAND = "万"; private static final String HUNDRED_MILLION = "亿"; private static final String YUAN = "元"; private static final String JIAO = "角"; private static final String FEN = "分"; private static final String DOT = "."; private static SimpleMoneyFormat formatter = null; private HashMap chineseNumberMap = new HashMap(); private HashMap chineseMoneyPattern = new HashMap(); private NumberFormat numberFormat = NumberFormat.getInstance(); private SimpleMoneyFormat() { numberFormat.setMaxmumFractionDigits(4); numberFormat.setMinmumFractionDigits(2); numberFormat.setGroupingUsed(false); chineseNumberMap.put("0", ZERO); chineseNumberMap.put("1", ONE); chineseNumberMap.put("2", TWO); chineseNumberMap.put("3", THREE); chineseNumberMap.put("4", FOUR); chineseNumberMap.put("5", FIVE); chineseNumberMap.put("6", SIX); chineseNumberMap.put("7", SEVEN); chineseNumberMap.put("8", EIGHT); chineseNumberMap.put("9", NINE); chineseNumberMap.put(".", DOT); chineseMoneyPattern.put("1", TEN); chineseMoneyPattern.put("2", HUNDRED); chineseMoneyPattern.put("3", THOUSAND); chineseMoneyPattern.put("4", TEN_THOUSAND); chineseMoneyPattern.put("5", TEN); chineseMoneyPattern.put("6", HUNDRED); chineseMoneyPattern.put("7", TEN_THOUSAND); chineseMoneyPattern.put("8", HUNDRED_MILLION); } public static SimpleMoneyFormat getInstance() { if(formatter == null) formatter = new SimpleMoneyFormat(); return formatter; } public String format(String moneyStr) { moneyStr = checkPrecision(moneyStr); String result; result = convertToChineseNumber(moneyStr); return result; } public String format(double moneyDouble ) { return format(numberFormat.format(moneyDouble)); } public String format(int moneyInt) { return format(numberFormat.format(moneyInt)); } public String format(long moneyLong) { return format(numberFormat.format(moneyLong)); } public String format(Number moneyNum) { return format(numberFormat.format(moneyNum)); } private String convertToChineseNumber(String moneyStr) { String result; StringBuffer cMoneyStringBuffer = new StringBuffer(); for(int i=0;i<moneyStr.length();i++) { cMoneyStringBuffer.append(chineseNumberMap.get(moneyStr.substring(i, i+1))); } int indexOfDot = cMoneyStringBuffer.indexOf(DOT); int moneyPatternCursor = 1; for(int i = indexOfDot - 1;i > 0;i--) { cMoneyStringBuffer.insert(i, chineseMoneyPattern.get(EMPTY + moneyPatternCursor)); moneyPatternCursor = (moneyPatternCursor == 8 ? 1 : moneyPatternCursor + 1); } String fractionPart = cMoneyStringBuffer.substring(cMoneyStringBuffer.indexOf(".")); StringBuffer cFractionPart = new StringBuffer(fractionPart); if(cFractionPart.length() == 3) { cFractionPart.replace(0, 1, YUAN); cFractionPart.insert(2, JIAO); cFractionPart.append(FEN); }else { cFractionPart.replace(0, 1, YUAN); cFractionPart.append("角零分"); } fractionPart = cFractionPart.toString(); cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf("."), cMoneyStringBuffer.length()); while(cMoneyStringBuffer.indexOf("零拾") != -1) { cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零拾"), cMoneyStringBuffer.indexOf("零拾" + 2), ZERO); } while(cMoneyStringBuffer.indexOf("零佰") != -1) { cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零佰"), cMoneyStringBuffer.indexOf("零佰") + 2, ZERO); } while(cMoneyStringBuffer.indexOf("零千") != -1) { cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零千"), cMoneyStringBuffer.indexOf("零千") + 2, ZERO); } while(cMoneyStringBuffer.indexOf("零万") != -1) { cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零万"), cMoneyStringBuffer.indexOf("零万") + 2, TEN_THOUSAND); } while(cMoneyStringBuffer.indexOf("零亿") != -1) { cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零亿"), cMoneyStringBuffer.indexOf("零亿") + 2, HUNDRED_MILLION); } while(cMoneyStringBuffer.indexOf("零零") != -1) { cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零零"), cMoneyStringBuffer.indexOf("零零") + 2, ZERO); } if(cMoneyStringBuffer.lastIndexOf(ZERO) == cMoneyStringBuffer.length() - 1) cMoneyStringBuffer.delete(cMoneyStringBuffer.length() - 1, cMoneyStringBuffer.length()); cMoneyStringBuffer.append(fractionPart); result = cMoneyStringBuffer.toString(); return result; } public String checkPrecision(String moneyStr) { if(moneyStr.indexOf(DOT) == -1) { moneyStr = moneyStr + ".0"; }else if(moneyStr.substring(moneyStr.indexOf(DOT)).length() > 3) { double moneyDouble = Double.parseDouble(moneyStr); BigDecimal moneyBigDecimal = new BigDecimal(moneyDouble).setScale(2, BigDecimal.ROUND_HALF_UP); moneyStr = moneyBigDecimal.toString(); } System.out.println("after checkPrecision:" + moneyStr); return moneyStr; } public static void main(String[] args) { System.out.println("Please input amount:"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String moneyStr; SimpleMoneyFormat smf = SimpleMoneyFormat.getInstance(); try { moneyStr = br.readLine(); System.out.println(smf.format(moneyStr)); } catch (IOException e) { e.printStackTrace(); } } } }