日期:2014-05-20 浏览次数:20904 次
private static final String[] digits = new String[]{" 零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; private static final String[] radices = new String[]{"", "拾", "佰", "仟"}; private static final String[] bigRadices = new String[]{"", "万", "亿", "万"}; /** * 算法:输入数字,将其转换为中文。 * * @param num * 数字字符串 * @return 转换结果(中文) */ public static String covnt(String num) { String integral = num; // 整数部分 String outputCharacters = ""; // 最终转换输出结果 String d = null; int zeroCount = 0, p = 0, quotient = 0, modulus = 0; for (int i = 0; i < integral.length(); i++) { p = integral.length() - i - 1; d = integral.substring(i, i + 1); quotient = p / 4; modulus = p % 4; if (d.equals("0")) { zeroCount++; } else { if (zeroCount > 0) { outputCharacters += digits[0]; } zeroCount = 0; outputCharacters += digits[Integer.parseInt(d)] + radices[modulus]; } if (modulus == 0 && zeroCount < 4) { outputCharacters += bigRadices[quotient]; } } return outputCharacters; }