日期:2014-05-20 浏览次数:20651 次
package com.demo; import java.util.HashMap; import java.util.Map; public class Test2 { /** * @param args */ public static void main(String[] args) { //int num = 190001; //String number = new Integer(num).toString(); //System.out.println(number.length()); //System.out.println(number.length() / 4); //System.out.println(number.length() % 4); int [] n = new int [] {30000,31000,120023201,120020001,100010001}; for(int num : n){ String number = new Integer(num).toString(); System.out.println(slipNum(number)); } } public static String slipNum(String formatNum){ StringBuffer sb = new StringBuffer(); if (formatNum.length() <= 4) { sb.append(format(formatNum)); }else if (formatNum.length() <=8 ) { sb.append(format(formatNum.substring(0,formatNum.length()-4))).append("万").append(slipNum(formatNum.substring(formatNum.length()-4,formatNum.length()))); }else if (formatNum.length() <=12 ) { sb.append(format(formatNum.substring(0,formatNum.length()-8))).append("亿").append(slipNum(formatNum.substring(formatNum.length()-8,formatNum.length()))); } return sb.toString(); } public static String format(String formatNum) { StringBuffer sb = new StringBuffer(); if (formatNum.length() == 1) sb.append(getChinaseNumber(formatNum)); else if (formatNum.length() == 2) { if (formatNum.endsWith("0")) sb.append(getChinaseNumber(formatNum)); else sb.append(getChinaseNumber(formatNum.substring(0, 1))).append("十").append(getChinaseNumber(formatNum.substring(1,2))); } else if (formatNum.length() == 3){ if (formatNum.startsWith("00")) sb.append(getChinaseNumber("00")).append(format(formatNum.substring(2,3))); else if (formatNum.startsWith("0")) sb.append(getChinaseNumber("0")).append(format(formatNum.substring(1,3))); else if (formatNum.endsWith("00")) sb.append(getChinaseNumber(formatNum)); else if (formatNum.endsWith("0")) sb.append(format(formatNum.substring(0,1))).append("百").append(format(formatNum.substring(1,3))); else if (formatNum.indexOf("0")!=-1) sb.append(getChinaseNumber(formatNum.substring(0, 1))).append("百").append(getChinaseNumber(formatNum.substring(1,2))).append(getChinaseNumber(formatNum.substring(2,3))); else sb.append(getChinaseNumber(formatNum.substring(0, 1))).append("百").append(getChinaseNumber(formatNum.substring(1,2))).append("十").append(getChinaseNumber(formatNum.substring(2,3))); }else if (formatNum.length() == 4){ if (formatNum.startsWith("0000")) sb.append(""); else if (formatNum.startsWith("000")) sb.append(getChinaseNumber("000")).append(format(formatNum.substring(3,4))); else if (formatNum.startsWith("00")) sb.append(getChinaseNumber("00")).append(format(formatNum.substring(2,4))); else if (formatNum.startsWith("0")) sb.append(getChinaseNumber("0")).append(format(formatNum.substring(1,4))); else if (formatNum.endsWith("000")) sb.append(getChinaseNumber(formatNum)); else if (formatNum.endsWith("00")||formatNum.endsWith("0")) sb.append(format(formatNum.substring(0,1))).append("千").append(format(formatNum.substring(1,4))); else if (formatNum.indexOf("00")!=-1) sb.append(getChinaseNumber(formatNum.substring(0, 1))).append("千").append(getChinaseNumber(formatNum.substring(1,3))).append(getChinaseNumber(formatNum.substring(3,4))); else if (formatNum.indexOf("0")!=-1) sb.append(getChinaseNumber(formatNum.substring(0, 1))).append("千").append(format(formatNum.substring(1,4))); else sb.append(getChinaseNumber(formatNum.substring(0, 1))).append("千").append(getChinaseNumber(formatNum.substring(1, 2))).append("百").append(getChinaseNumber(formatNum.substring(2,3))).append("十").append(getChinaseNumber(formatNum.substring(4,4))); } return sb.toString(); } public static String getChinaseNumber(String n) { Map<String, String> map = new HashMap(); map.put("1", "一"); map.put("2", "二"); map.put("3", "三"); map.put("4", "四"); map.put("5", "五"); map.put("6", "六"); map.put("7", "七"); map.put("8", "八"); map.put("9", "九"); map.put("10", "一十"); map.put("100", "一百"); map.put("1000", "一千"); map.put("20", "二十"); map.put("200", "二百"); map.put("2000", "二千"); map.put("30", "三十"); map.put("300", "三百"); map.put("3000", "三千"); map.put("40", "四十"); map.put("400", "四百"); map.put("4000", "四千"); map.put("50", "五十"); map.put("500", "五百"); map.put("5000", "五千"); map.put("60", "六十"); map.put("600", "六百"); map.put("6000", "六千"); map.put("70", "七十"); map.put("700", "七百"); map.put("7000", "七千"); map.put("80", "八十"); map.put("800", "八百"); map.put("8000", "八千"); map.put("90", "九十"); map.put("900", "九百"); map.put("9000", "九千"); map.put("0", "零"); map.put("00", "零"); map.put("000", "零"); map.put("0000", "零"); return map.get(n + ""); } }