1,数字转大写
package org.xxx.util;import java.util.ArrayList;import java.util.Collections;import java.util.regex.Pattern;public class NumToCase { /** * 单位进位,中文默认为4位即(万、亿) */ public static int UNIT_STEP = 4; /** * 单位 */ public static String[] CN_UNITS = new String[] { "个", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千", "万" }; /** * 汉字 */ public static String[] CN_CHARS = new String[] { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" }; /** * 将阿拉伯数字转换为中文数字123=》一二三 * * @param srcNum * @return */ public static String getCNNum(int srcNum) { String desCNNum = ""; if (srcNum <= 0) desCNNum = "零"; else { int singleDigit; while (srcNum > 0) { singleDigit = srcNum % 10; desCNNum = String.valueOf(CN_CHARS[singleDigit]) + desCNNum; srcNum /= 10; } } return desCNNum; } /** * 数值转换为中文字符串 如2转化为贰 */ public String cvt(long num) { return this.cvt(num, false); } /** * 数值转换为中文字符串(口语化) * * @param num * 需要转换的数值 * @param isColloquial * 是否口语化。例如12转换为'十二'而不是'一十二'。 * @return */ public String cvt(String num, boolean isColloquial) { int integer, decimal; StringBuffer strs = new StringBuffer(32); String[] splitNum = num.split("\\."); integer = Integer.parseInt(splitNum[0]); //整数部分 decimal = Integer.parseInt(splitNum[1]); //小数部分 String[] result_1 =convert(integer, isColloquial); for (String str1 : result_1) strs.append(str1); if(decimal==0){//小数部分为0时 return strs.toString(); }else{ String result_2 =getCNNum(decimal); //例如5.32,小数部分展示三二,而不是三十二 strs.append("点"); strs.append(result_2); return strs.toString(); } } /* * 对于int,long类型的数据处理 */ public String cvt(long num, boolean isColloquial) { String[] result = this.convert(num, isColloquial); StringBuffer strs = new StringBuffer(32); for (String str : result) { strs.append(str); } return strs.toString(); } /** * 将数值转换为中文 * * @param num * 需要转换的数值 * @param isColloquial * 是否口语化。例如12转换为'十二'而不是'一十二'。 * @return */ public String[] convert(long num, boolean isColloquial) { if (num < 10) {// 10以下直接返回对应汉字 return new String[] { CN_CHARS[(int) num] };// ASCII2int } char[] chars = String.valueOf(num).toCharArray(); if (chars.length > CN_UNITS.length) {// 超过单位表示范围的返回空 return new String[] {}; } boolean isLastUnitStep = false;// 记录上次单位进位 ArrayList<String> cnchars = new ArrayList<String>(chars.length * 2);// 创建数组,将数字填入单位对应的位置 for (int pos = chars.length - 1; pos >= 0; pos--) {// 从低位向高位循环 char ch = chars[pos]; String cnChar = CN_CHARS[ch - '0'];// ascii2int 汉字 int unitPos = chars.length - pos - 1;// 对应的单位坐标 String cnUnit = CN_UNITS[unitPos];// 单位 boolean isZero = (ch == '0');// 是否为0 boolean isZeroLow = (pos + 1 < chars.length && chars[pos + 1] == '0');// 是否低位为0 boolean isUnitStep = (unitPos >= UNIT_STEP && (unitPos % UNIT_STEP == 0));// 当前位是否需要单位进位 if (isUnitStep && isLastUnitStep) {// 去除相邻的上一个单位进位 int size = cnchars.size(); cnchars.remove(size - 1); if (!CN_CHARS[0].equals(cnchars.get(size - 2))) {// 补0 cnchars.add(CN_CHARS[0]); } } if (isUnitStep || !isZero) {// 单位进位(万、亿),或者非0时加上单位 cnchars.add(cnUnit); isLastUnitStep = isUnitStep; } if (isZero && (isZeroLow || isUnitStep)) {// 当前位为0低位为0,或者当前位为0并且为单位进位时进行省略 continue; } cnchars.add(cnChar); isLastUnitStep = false; } Collections.reverse(cnchars); // 清除最后一位的0 int chSize = cnchars.size(); String chEnd = cnchars.get(chSize - 1); if (CN_CHARS[0].equals(chEnd) || CN_UNITS[0].equals(chEnd)) { cnchars.remove(chSize - 1); } // 口语化处理 if (isColloquial) { String chFirst = cnchars.get(0); String chSecond = cnchars.get(1); if (chFirst.equals(CN_CHARS[1]) && chSecond.startsWith(CN_UNITS[1])) {// 是否以'一'开头,紧跟'十' cnchars.remove(0); } } return cnchars.toArray(new String[] {}); } public static void main(String[] args) { NumToCase temp = new NumToCase(); String i ="40.3"; int t = 2234; double j = 221.23; System.out.println(temp.cvt(i,true));//四十点三 System.out.println(temp.cvt(t,true));//二千二百三十四 System.out.println(temp.cvt(""+j,true));//二百二十一点二三 }}
2,数字转汉字
package org.xxx.util;import java.util.StringTokenizer;public class StringHelper { public static String[] strToStrArray(String str, String separator) { return strToStrArrayManager(str, separator); } private static String[] strToStrArrayManager(String str, String separator) { StringTokenizer strTokens = new StringTokenizer(str, separator); String[] strArray = new String[strTokens.countTokens()]; int i = 0; while (strTokens.hasMoreTokens()) { strArray[i] = strTokens.nextToken().trim(); i++; } return strArray; }}
package org.xxx.util;import java.text.DecimalFormat;import java.util.HashMap;import java.util.Map;public class NumToChinese { private static final String[] BEFORE_SCALE = { "万", "仟", "佰", "拾", "亿", "仟", "佰", "拾", "万", "仟", "佰", "拾", "" };// private static final String[] AFTER_SCALE = { "角", "分" }; private static final String[] AFTER_SCALE = { "", "" }; private static final String DEFAULT_PATH_SEPARATOR = "."; private static final Map<String, String> NUMBER_MAPPING = new HashMap<String, String>(); static { NUMBER_MAPPING.put("0", "零"); NUMBER_MAPPING.put("1", "壹"); NUMBER_MAPPING.put("2", "贰"); NUMBER_MAPPING.put("3", "叁"); NUMBER_MAPPING.put("4", "肆"); NUMBER_MAPPING.put("5", "伍"); NUMBER_MAPPING.put("6", "陆"); NUMBER_MAPPING.put("7", "柒"); NUMBER_MAPPING.put("8", "捌"); NUMBER_MAPPING.put("9", "玖"); } public static String getChineseNumber(String number) { return getChineseNumber(number, null, null); } public static String getChineseNumber(String number, String unit, String postfix) { String[] numbers = StringHelper.strToStrArray(number, DEFAULT_PATH_SEPARATOR); if (numbers.length > 2) { new NumberFormatException("数字格式错误!"); } int length = numbers[0].length(); int isZero = 0; StringBuffer result = new StringBuffer(); for (int i = 0; i < length; i++) { String digit = String.valueOf(numbers[0].charAt(i)); boolean allZero = true; // 如果后继的全部是零,则跳出 for (int j = i; j < length; j++) { if (numbers[0].charAt(j) != '0') { allZero = false; break; } } if (allZero) { boolean hasValue = false; for (int z = i; z >= 0; z--) { if (numbers[0].charAt(z) != '0' && length - z <= 7 && length - z >= 5) { hasValue = true; break; } } // 加万单位 if ( ( length - i > 4 && length <= 8 ) || ( hasValue && length - i > 4 )) { result.append(BEFORE_SCALE[8]); } // 加亿单位 if (length - i >= 9) { result.append(BEFORE_SCALE[4]); } break; } if (length < 9 && length - i == 5) { if (!"0".equals(digit) && isZero > 0) { result.append(NUMBER_MAPPING.get("0")); } if ("0".equals(digit)) { result.append(BEFORE_SCALE[8]); if (isZero > 0) { result.append(NUMBER_MAPPING.get("0")); } continue; } } if ("0".equals(digit) && length > 9 && length - i == 9) { result.append(BEFORE_SCALE[4]); continue; } if (isZero < 1 || !"0".equals(digit)) { if ("0".equals(digit)) { if (length - i != 6 && length - i != 7) { result.append(NUMBER_MAPPING.get(digit)); } } else { result.append(NUMBER_MAPPING.get(digit)); } if (!"0".equals(digit)) { result.append(BEFORE_SCALE[BEFORE_SCALE.length - length + i]); } } if ("0".equals(digit)) { isZero++; } else { isZero = 0; } }// result.append(unit == null ? "圆" : result.append(unit)); result.append(unit == null ? "" : result.append(unit)); if (numbers.length == 1) {// result.append(postfix == null ? "整" : result.append(postfix)); result.append(postfix == null ? "" : result.append(postfix)); return result.toString(); } length = numbers[1].length(); for (int j = 0; j < length; j++) { if (j > 2) { break; } if (numbers[1].charAt(j) == '0') { continue; } result.append(NUMBER_MAPPING.get(String.valueOf(numbers[1].charAt(j)))); result.append(AFTER_SCALE[j]); }// result.append(postfix == null ? "整" : result.append(postfix)); result.append(postfix == null ? "" : result.append(postfix)); return result.toString(); } public static String getChineseNumber(int number) { return getChineseNumber(new Integer(number)); } public static String getChineseNumber(int number, String unit, String postfix) { return getChineseNumber(new Integer(number), unit, postfix); } public static String getChineseNumber(Long number) { return getChineseNumber(number.toString(), null, null); } public static String getChineseNumber(Integer number) { return getChineseNumber(number.toString(), null, null); } public static String getChineseNumber(Integer number, String unit, String postfix) { return getChineseNumber(number.toString(), unit, postfix); } public static String getChineseNumber(Long number, String unit, String postfix) { return getChineseNumber(number.toString(), unit, postfix); } public static String getChineseNumber(long number) { return getChineseNumber(new Long(number)); } public static String getChineseNumber(long number, String unit, String postfix) { return getChineseNumber(new Long(number), unit, postfix); } public static String getChineseNumber(double number, String unit, String postfix) { DecimalFormat f = (DecimalFormat) DecimalFormat.getInstance(); f.applyLocalizedPattern("#.##"); return getChineseNumber(f.format(number), unit, postfix); } public static String getChineseNumber(double number) { return getChineseNumber(number, null, null); } public static String getChineseNumber(Double number) { return getChineseNumber(number.doubleValue()); } public static String getChineseNumber(Double number, String unit, String postfix) { return getChineseNumber(number.doubleValue(), unit, postfix); } public static void main(String[] args) { System.out.println(getChineseNumber(2019)); System.out.println(getChineseNumber(2345.632234)); System.out.println(getChineseNumber(2345.68657)); System.out.println(getChineseNumber(235667)); }}